Skip to content

Commit

Permalink
init func added
Browse files Browse the repository at this point in the history
  • Loading branch information
nxy7 committed Jun 20, 2023
1 parent e81e21b commit 9d59901
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 97 deletions.
1 change: 0 additions & 1 deletion cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
40 changes: 10 additions & 30 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
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")
},
}

Expand Down
13 changes: 3 additions & 10 deletions cmd/printConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,22 @@ 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)
},
}

func init() {
AppConfig = config.LoadConfig()
rootCmd.AddCommand(printConfigCmd)

// Here you will define your flags and configuration settings.
Expand Down
16 changes: 16 additions & 0 deletions config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 24 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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{},
}
}
14 changes: 13 additions & 1 deletion config/global.go
Original file line number Diff line number Diff line change
@@ -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,
}

}
40 changes: 12 additions & 28 deletions config/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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
}
}
5 changes: 0 additions & 5 deletions config/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 0 additions & 13 deletions config_test.go

This file was deleted.

5 changes: 4 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down
4 changes: 2 additions & 2 deletions optimus.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
global:
shell_cmd: bash -c
# global:
# shell_cmd: bash -c

include:
- ./config
Expand Down

0 comments on commit 9d59901

Please sign in to comment.