diff --git a/cmd/dev.go b/cmd/dev.go index b797251..0eff29a 100644 --- a/cmd/dev.go +++ b/cmd/dev.go @@ -18,7 +18,6 @@ var devCmd = &cobra.Command{ } func init() { - AppConfig = config.LoadConfig() for n, t := range AppConfig.Services { devCmd.AddCommand(&cobra.Command{ Use: n, diff --git a/cmd/init.go b/cmd/init.go index 16ce25c..666d944 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,47 +1,27 @@ /* Copyright © 2023 NAME HERE + */ package cmd import ( - "optimus/utils" - "os" - "os/exec" + "fmt" "github.com/spf13/cobra" - "github.com/spf13/viper" ) // initCmd represents the init command var initCmd = &cobra.Command{ Use: "init", - Short: "Initialize project environment", - Long: `Initialize project environment. This commands runs 'init' script found in 'optimus' config file until completion.`, - Run: func(cmd *cobra.Command, args []string) { - dirPath := utils.ProjectRoot() - viper.SetConfigType("yaml") - viper.SetConfigName("optimus") - viper.AddConfigPath(dirPath) - - err := viper.ReadInConfig() - if err != nil { - println(err) - } - init := viper.GetString("init") - c := exec.Command("bash", "-c", init) - c.Stdout = os.Stdout - c.Stderr = os.Stderr - err = c.Run() - if err != nil { - // println("command failed: ", err) - println(err.Error()) - } - - // initByLine := strings.Split(init, "\n") - // for _, line := range initByLine { - // println(line) - // } + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("init called") }, } diff --git a/cmd/printConfig.go b/cmd/printConfig.go index a560c78..db8c6b4 100644 --- a/cmd/printConfig.go +++ b/cmd/printConfig.go @@ -5,21 +5,15 @@ package cmd import ( "fmt" - "optimus/config" "github.com/spf13/cobra" ) // printConfigCmd represents the printConfig command var printConfigCmd = &cobra.Command{ - Use: "printConfig", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Use: "print-config", + Aliases: []string{"pc"}, + Short: "Print merged Optimus Configuration", Run: func(cmd *cobra.Command, args []string) { fmt.Println("Printing config") fmt.Printf("AppConfig: %+v\n", AppConfig) @@ -27,7 +21,6 @@ to quickly create a Cobra application.`, } func init() { - AppConfig = config.LoadConfig() rootCmd.AddCommand(printConfigCmd) // Here you will define your flags and configuration settings. diff --git a/config/command.go b/config/command.go index e19bf6b..c9f3f88 100644 --- a/config/command.go +++ b/config/command.go @@ -5,6 +5,22 @@ type Cmd struct { File string Shell string } + +func ParseCmd(a any) Cmd { + cmd := Cmd{ + Cmd: "", + File: "", + Shell: "", + } + s, ok := a.(string) + if ok { + cmd.Cmd = s + return cmd + } + + panic("Invalid Cmd shape") +} + type TestCmd struct { Cmd Cmd DependsOn []Service diff --git a/config/config.go b/config/config.go index e0d3f41..1dd1d98 100644 --- a/config/config.go +++ b/config/config.go @@ -1,10 +1,10 @@ package config type Config struct { - Global Global - Include []string - Init any - E2eTests any + Global Global + // Include []string + Init Cmd + E2eTests Cmd Purge Cmd Services map[string]any AdditionalCommands []Cmd @@ -15,9 +15,27 @@ type E2eTests struct { } func LoadConfig() Config { - _ = LoadRawConfig() + var conf = DefaultConfig() + // raw := LoadRawConfig() + // if raw.Global.(map[string]any) { + // conf.Global = ParseGlobal(raw.Global) + // } + // init := ParseCmd(raw.Init) + // global := raw. // parse raw config - var conf = Config{} return conf } + +func DefaultConfig() Config { + return Config{ + Global: Global{ + ShellCmd: "bash -c", + }, + Init: Cmd{}, + E2eTests: Cmd{}, + Purge: Cmd{}, + Services: map[string]any{}, + AdditionalCommands: []Cmd{}, + } +} diff --git a/config/global.go b/config/global.go index 7b06960..fa5cd1f 100644 --- a/config/global.go +++ b/config/global.go @@ -1,5 +1,17 @@ package config type Global struct { - ShellCmd string `mapstructure:"shell_cmd"` + ShellCmd string +} + +func ParseGlobal(a any) Global { + temp, ok := a.(map[string]any) + if !ok { + panic("Invalid Global Propety") + } + val := temp["shell_cmd"].(string) + return Global{ + ShellCmd: val, + } + } diff --git a/config/raw.go b/config/raw.go index df6f87b..d8cb384 100644 --- a/config/raw.go +++ b/config/raw.go @@ -10,24 +10,24 @@ import ( ) // If config field has few shapes (like string or some struct) we're using any to parse it into said struct later on -type rawConfig struct { +type RawConfig struct { Global any `mapstructure:"global"` Include []string `mapstructure:"include"` Init any `mapstructure:"init"` E2e_Tests any `mapstructure:"e2e_tests"` Services map[string]any `mapstructure:"services"` Purge any `mapstructure:"purge"` - Cmds []any `mapstructure:"cmds"` + Cmds map[string]any `mapstructure:"cmds"` } -func LoadRawConfig() rawConfig { +func LoadRawConfig() RawConfig { dirPath := utils.ProjectRoot() c := loadConfigFromPath(dirPath) return c } -func loadConfigFromPath(p string) rawConfig { +func loadConfigFromPath(p string) RawConfig { v := viper.New() v.SetConfigType("yaml") v.SetConfigName("optimus") @@ -39,7 +39,7 @@ func loadConfigFromPath(p string) rawConfig { log.Fatalf("Could not read config at path: %v\n%v", p, err) } - var c rawConfig + var c RawConfig err = v.Unmarshal(&c) if err != nil { fmt.Println(err) @@ -51,35 +51,19 @@ func loadConfigFromPath(p string) rawConfig { return c } -func (rc *rawConfig) loadIncludes(dirPath string) { +func (rc *RawConfig) loadIncludes(dirPath string) { for _, v := range rc.Include { newPath := strings.Replace(v, "./", "/", 1) loadedConfig := loadConfigFromPath(dirPath + newPath) - new := mergeRawConfigs(*rc, loadedConfig) - *rc = new + rc.mergeRawConfigs(loadedConfig) } } -func mergeRawConfigs(c1 rawConfig, c2 rawConfig) rawConfig { - if c1.Global != nil && c2.Global != nil { - panic("Global field specified multiple times") +func (rc *RawConfig) mergeRawConfigs(newConfig RawConfig) { + for k, v := range newConfig.Services { + rc.Services[k] = v } - - if c1.Init != nil && c2.Init != nil { - panic("Init field specified multiple times") - } - - if c1.Init != nil && c2.Init != nil { - panic("Init field specified multiple times") - } - - return rawConfig{ - Global: c1.Global, - Include: []string{}, - Init: nil, - E2e_Tests: nil, - Services: map[string]any{}, - Purge: nil, - Cmds: []any{}, + for i, v := range newConfig.Cmds { + rc.Services[i] = v } } diff --git a/config/raw_test.go b/config/raw_test.go index 8500f3f..25bbb85 100644 --- a/config/raw_test.go +++ b/config/raw_test.go @@ -7,10 +7,5 @@ import ( func TestRawConfig(t *testing.T) { c := LoadRawConfig() - // t.Logf("config", c) - // t.Logf(c) fmt.Printf("%+v\n", c) - // fmt.Printf("c: %v\n", c) - // fmt.Println(c) - // fmt.Fprintln(os.Stdout, "config: ", c) } diff --git a/config_test.go b/config_test.go deleted file mode 100644 index 966f8c8..0000000 --- a/config_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "testing" -) - -func TestConfigDefaults(t *testing.T) { - -} - -func TestConfigParsing(t *testing.T) { - // todo -} diff --git a/flake.nix b/flake.nix index dd3506c..68fbcca 100644 --- a/flake.nix +++ b/flake.nix @@ -20,13 +20,16 @@ pname = "optimus"; version = "0.0.1"; + # we're looking for .git folder during tests, so they will fail in nix environment + doCheck = false; + src = ./.; vendorHash = "sha256-3tO/+Mnvl/wpS7Ro3XDIVrlYTGVM680mcC15/7ON6qM="; # vendorHash = pkgs.lib.fakeHash; meta = with pkgs.lib; { - description = "Simple command-line snippet manager, written in Go"; + description = "CLI management tool"; homepage = "https://github.com/nxy7/optimus"; license = licenses.mit; maintainers = with maintainers; [ nxyt ]; diff --git a/optimus.yaml b/optimus.yaml index 55151dc..3ef8d7c 100644 --- a/optimus.yaml +++ b/optimus.yaml @@ -1,5 +1,5 @@ -global: - shell_cmd: bash -c +# global: +# shell_cmd: bash -c include: - ./config