2022-02-24 22:38:17 +01:00
package cmd
import (
"log"
"os"
2022-02-28 23:41:21 +01:00
"strconv"
"time"
2022-02-24 22:38:17 +01:00
2022-09-02 22:42:33 +02:00
mapper "gitea.antoine-langlois.net/datahearth/config-mapper/internal"
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/configuration"
"gitea.antoine-langlois.net/datahearth/config-mapper/internal/git"
2022-04-07 20:08:44 +02:00
"github.com/fatih/color"
2022-02-24 22:38:17 +01:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
errLogger = log . New ( os . Stderr , "" , 0 )
logger = log . New ( os . Stderr , "" , 0 )
)
var rootCmd = & cobra . Command {
Use : "config-mapper" ,
Short : "Manage your systems configuration" ,
Long : ` config - mapper aims to help you manage your configurations between systems
with a single configuration file . ` ,
2022-08-21 01:40:20 +02:00
Version : "v0.6.0" ,
2022-02-24 22:38:17 +01:00
}
var initCmd = & cobra . Command {
Use : "init" ,
Short : "Initialize your configuration folder" ,
Long : ` Initialize will retrieve your configuration folder from the source location and
copy it into the destination field ` ,
2022-05-23 09:09:59 +02:00
Run : initCommand ,
2022-02-24 22:38:17 +01:00
}
2022-02-25 00:17:02 +01:00
var loadCmd = & cobra . Command {
Use : "load" ,
Short : "Load your configurations onto your system" ,
Long : ` Load your files , folders and package managers deps configurations onto your new
onto your new system based on your configuration file ` ,
2022-05-23 09:09:59 +02:00
Run : load ,
2022-02-24 22:38:17 +01:00
}
2022-02-26 15:46:14 +01:00
var saveCmd = & cobra . Command {
Use : "save" ,
Short : "save your configurations onto your saved location" ,
Long : ` Save your files , folders and package managers deps configurations onto your
saved location based on your configuration file ` ,
2022-05-23 09:09:59 +02:00
Run : save ,
2022-02-26 15:46:14 +01:00
}
2022-02-24 22:38:17 +01:00
2022-02-25 00:17:02 +01:00
func init ( ) {
2022-06-02 19:44:51 +02:00
cobra . OnInitialize ( configuration . InitConfig )
2022-02-24 22:38:17 +01:00
2022-02-25 00:17:02 +01:00
rootCmd . AddCommand ( initCmd )
rootCmd . AddCommand ( loadCmd )
2022-02-26 15:45:10 +01:00
rootCmd . AddCommand ( saveCmd )
2022-07-10 12:35:01 +02:00
2022-06-06 15:32:01 +02:00
rootCmd . PersistentFlags ( ) . BoolP ( "verbose" , "v" , false , "STDOUT will be more verbose" )
2022-02-26 15:45:10 +01:00
rootCmd . PersistentFlags ( ) . StringP ( "configuration-file" , "c" , "" , "location of configuration file" )
2022-06-02 19:44:51 +02:00
rootCmd . PersistentFlags ( ) . String ( "ssh-user" , "" , "SSH username to retrieve configuration file" )
rootCmd . PersistentFlags ( ) . String ( "ssh-password" , "" , "SSH password to retrieve configuration file" )
rootCmd . PersistentFlags ( ) . String ( "ssh-key" , "" , "SSH key to retrieve configuration file (if a passphrase is needed, use the \"CONFIG_MAPPER_PASS\" env variable" )
2022-06-06 15:32:01 +02:00
viper . BindPFlag ( "verbose" , rootCmd . PersistentFlags ( ) . Lookup ( "verbose" ) )
2022-02-26 15:45:10 +01:00
viper . BindPFlag ( "configuration-file" , rootCmd . PersistentFlags ( ) . Lookup ( "configuration-file" ) )
2022-06-02 19:44:51 +02:00
viper . BindPFlag ( "ssh-user" , rootCmd . PersistentFlags ( ) . Lookup ( "ssh-user" ) )
viper . BindPFlag ( "ssh-password" , rootCmd . PersistentFlags ( ) . Lookup ( "ssh-password" ) )
viper . BindPFlag ( "ssh-key" , rootCmd . PersistentFlags ( ) . Lookup ( "ssh-key" ) )
loadCmd . Flags ( ) . Bool ( "disable-files" , false , "files will be ignored" )
loadCmd . Flags ( ) . Bool ( "disable-folders" , false , "folders will be ignored" )
loadCmd . Flags ( ) . Bool ( "pkgs" , false , "packages will be installed" )
2022-07-20 13:33:04 +02:00
loadCmd . Flags ( ) . StringSlice ( "exclude-pkg-managers" , [ ] string { } , "package managers to exclude (comma separated)" )
2022-06-02 19:44:51 +02:00
viper . BindPFlag ( "load-disable-files" , loadCmd . Flags ( ) . Lookup ( "disable-files" ) )
viper . BindPFlag ( "load-disable-folders" , loadCmd . Flags ( ) . Lookup ( "disable-folders" ) )
viper . BindPFlag ( "load-enable-pkgs" , loadCmd . Flags ( ) . Lookup ( "pkgs" ) )
2022-07-20 13:33:04 +02:00
viper . BindPFlag ( "exclude-pkg-managers" , loadCmd . Flags ( ) . Lookup ( "exclude-pkg-managers" ) )
2022-06-02 19:44:51 +02:00
saveCmd . Flags ( ) . Bool ( "disable-files" , false , "files will be ignored" )
saveCmd . Flags ( ) . Bool ( "disable-folders" , false , "folders will be ignored" )
2022-02-28 23:41:21 +01:00
saveCmd . Flags ( ) . BoolP ( "push" , "p" , false , "new configurations will be committed and pushed" )
saveCmd . Flags ( ) . StringP ( "message" , "m" , strconv . FormatInt ( time . Now ( ) . Unix ( ) , 10 ) , "combined with --push to set a commit message" )
2022-04-07 20:08:44 +02:00
saveCmd . Flags ( ) . Bool ( "disable-index" , false , "configuration index will not be updated" )
2022-06-02 19:44:51 +02:00
viper . BindPFlag ( "save-disable-files" , saveCmd . Flags ( ) . Lookup ( "disable-files" ) )
viper . BindPFlag ( "save-disable-folders" , saveCmd . Flags ( ) . Lookup ( "disable-folders" ) )
2022-02-28 23:41:21 +01:00
viper . BindPFlag ( "push" , saveCmd . Flags ( ) . Lookup ( "push" ) )
2022-04-07 20:08:44 +02:00
viper . BindPFlag ( "disable-index-update" , saveCmd . Flags ( ) . Lookup ( "disable-index" ) )
2022-02-28 23:41:21 +01:00
viper . BindPFlag ( "message" , saveCmd . Flags ( ) . Lookup ( "message" ) )
2022-02-24 22:38:17 +01:00
}
func Execute ( ) {
if err := rootCmd . Execute ( ) ; err != nil {
errLogger . Printf ( "an error occured while running command: %v\n" , err )
os . Exit ( 1 )
}
}
2022-05-23 09:09:59 +02:00
func save ( cmd * cobra . Command , args [ ] string ) {
2022-06-02 19:44:51 +02:00
var c configuration . Configuration
2022-05-23 09:09:59 +02:00
if err := viper . Unmarshal ( & c ) ; err != nil {
mapper . PrintError ( "failed to decode configuration: %v\n" , err )
os . Exit ( 1 )
}
indexer , err := mapper . NewIndexer ( c . Storage . Path )
if err != nil {
mapper . PrintError ( "failed to open the indexer: %v\n" , err )
os . Exit ( 1 )
}
2022-06-02 19:44:51 +02:00
r , err := git . NewRepository ( c . Storage . Git , c . Storage . Path )
2022-05-23 09:09:59 +02:00
if err != nil {
mapper . PrintError ( "failed to open repository at %s: %v\n" , c . Storage . Path , err )
os . Exit ( 1 )
}
el := mapper . NewItemsActions ( nil , c . Storage . Path , r , indexer )
if ! viper . GetBool ( "save-disable-files" ) {
el . AddItems ( c . Files )
}
if ! viper . GetBool ( "save-disable-folders" ) {
el . AddItems ( c . Folders )
}
el . Action ( "save" )
if err := el . CleanUp ( indexer . RemovedLines ( ) ) ; err != nil {
mapper . PrintError ( "failed to clean repository: %v\n" , err )
os . Exit ( 1 )
}
if viper . GetBool ( "push" ) {
color . Blue ( "# Pushing items" )
if err := r . PushChanges ( viper . GetString ( "message" ) , indexer . Lines ( ) , indexer . RemovedLines ( ) ) ; err != nil {
mapper . PrintError ( "failed to push changes to repository: %v\n" , err )
os . Exit ( 1 )
}
color . Green ( "Items pushed" )
}
}
func load ( cmd * cobra . Command , args [ ] string ) {
2022-06-02 19:44:51 +02:00
var c configuration . Configuration
2022-05-23 09:09:59 +02:00
if err := viper . Unmarshal ( & c ) ; err != nil {
mapper . PrintError ( "failed to decode configuration: %v\n" , err )
os . Exit ( 1 )
}
i , err := mapper . NewIndexer ( c . Storage . Path )
if err != nil {
mapper . PrintError ( "failed to open the indexer: %v\n" , err )
os . Exit ( 1 )
}
2022-06-02 19:44:51 +02:00
r , err := git . NewRepository ( c . Storage . Git , c . Storage . Path )
2022-05-23 09:09:59 +02:00
if err != nil {
mapper . PrintError ( "failed to open repository at %s: %v\n" , c . Storage . Path , err )
os . Exit ( 1 )
}
el := mapper . NewItemsActions ( nil , c . Storage . Path , r , i )
if ! viper . GetBool ( "load-disable-files" ) {
el . AddItems ( c . Files )
}
if ! viper . GetBool ( "load-disable-folders" ) {
el . AddItems ( c . Folders )
}
el . Action ( "load" )
2022-06-01 11:47:33 +02:00
if viper . GetBool ( "load-enable-pkgs" ) {
2022-07-10 12:35:01 +02:00
if err := mapper . InstallPackages ( c . PackageManagers ) ; err != nil {
2022-05-23 09:09:59 +02:00
mapper . PrintError ( err . Error ( ) )
os . Exit ( 1 )
}
}
}
func initCommand ( cmd * cobra . Command , args [ ] string ) {
2022-06-02 19:44:51 +02:00
var c configuration . Configuration
if err := viper . Unmarshal ( & c ) ; err != nil {
2022-05-23 09:09:59 +02:00
mapper . PrintError ( "failed to decode configuration: %v\n" , err )
os . Exit ( 1 )
}
logger . Println ( "initializing config-mapper folder from configuration..." )
2022-06-02 19:44:51 +02:00
if _ , err := git . NewRepository ( c . Storage . Git , c . Storage . Path ) ; err != nil {
2022-05-23 09:09:59 +02:00
mapper . PrintError ( "failed to initialize folder: %v\n" , err )
os . Exit ( 1 )
}
logger . Printf ( "repository initialized at \"%v\"\n" , viper . GetString ( "storage.location" ) )
}