Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add save config feature within the config subcommand #315

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions calnex/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var (
insecureTLS bool
source string
target string
saveConfig string
)

// Execute is the main entry point for CLI interface
Expand Down
12 changes: 10 additions & 2 deletions calnex/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
configCmd.Flags().BoolVar(&insecureTLS, "insecureTLS", false, "Ignore TLS certificate errors")
configCmd.Flags().StringVar(&target, "target", "", "device to configure")
configCmd.Flags().StringVar(&source, "file", "", "configuration file")
configCmd.Flags().StringVar(&saveConfig, "save", "", "save configuration to the specified path")
if err := configCmd.MarkFlagRequired("target"); err != nil {
log.Fatal(err)
}
if err := configCmd.MarkFlagRequired("file"); err != nil {
log.Fatal(err)
}
configCmd.MarkFlagsMutuallyExclusive("apply", "save")

Check failure on line 42 in calnex/cmd/config.go

View workflow job for this annotation

GitHub Actions / test

configCmd.MarkFlagsMutuallyExclusive undefined (type *cobra.Command has no field or method MarkFlagsMutuallyExclusive)

Check failure on line 42 in calnex/cmd/config.go

View workflow job for this annotation

GitHub Actions / lint

configCmd.MarkFlagsMutuallyExclusive undefined (type *cobra.Command has no field or method MarkFlagsMutuallyExclusive) (typecheck)
}

var configCmd = &cobra.Command{
Expand Down Expand Up @@ -65,8 +67,14 @@
log.Fatalf("Failed to find config for %s in %s", target, source)
}

if err := config.Config(target, insecureTLS, dc, apply); err != nil {
log.Fatal(err)
if saveConfig != "" {
if err := config.Save(target, insecureTLS, dc, saveConfig); err != nil {
log.Fatal(err)
}
} else {
if err := config.Config(target, insecureTLS, dc, apply); err != nil {
log.Fatal(err)
}
}
},
}
50 changes: 40 additions & 10 deletions calnex/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,12 @@ func Config(target string, insecureTLS bool, cc *CalnexConfig, apply bool) error
var c config
api := api.NewAPI(target, insecureTLS)

f, err := api.FetchSettings()
f, err := prepare(&c, api, target, cc)

if err != nil {
return err
}

m := f.Section("measure")
g := f.Section("gnss")

// set base config
c.baseConfig(m, g, target, cc.AntennaDelayNS)

// set measure config
c.measureConfig(m, cc.Measure)

if !apply {
log.Info("dry run. Exiting")
return nil
Expand Down Expand Up @@ -246,3 +238,41 @@ func Config(target string, insecureTLS bool, cc *CalnexConfig, apply bool) error

return nil
}

// Save saves the Network/Calnex configs to file
func Save(target string, insecureTLS bool, cc *CalnexConfig, saveConfig string) error {
var c config
api := api.NewAPI(target, insecureTLS)

f, err := prepare(&c, api, target, cc)

if err != nil {
return err
}

err = f.SaveTo(saveConfig)

if err != nil {
return err
}

return nil
}

func prepare(c *config, api *api.API, target string, cc *CalnexConfig) (*ini.File, error) {
f, err := api.FetchSettings()
if err != nil {
return nil, err
}

m := f.Section("measure")
g := f.Section("gnss")

// set base config
c.baseConfig(m, g, target, cc.AntennaDelayNS)

// set measure config
c.measureConfig(m, cc.Measure)

return f, nil
}
Loading
Loading