Skip to content

Commit

Permalink
Merge pull request #80 from RitchieS/fix/save-config
Browse files Browse the repository at this point in the history
Fix save-config to overwrite properly
  • Loading branch information
RitchieS committed Oct 26, 2023
2 parents f9d89b5 + 51e51b8 commit 7243234
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
53 changes: 41 additions & 12 deletions cmd/ctfd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func ctfdOptions() {
opts.UnsolvedOnly = viper.GetBool("unsolved")
opts.Notify = viper.GetBool("notify")
opts.MaxFileSize = viper.GetInt64("max-file-size")
options.RateLimit = viper.GetInt("rate-limit")
}

func getBaseURL(cmd *cobra.Command) *url.URL {
Expand Down Expand Up @@ -98,21 +99,49 @@ func setupOutputFolder() string {

func saveConfig() {
viper.Reset()
viper.Set("url", opts.URL)
viper.Set("username", opts.Username)
viper.Set("password", opts.Password)
viper.Set("token", opts.Token)
viper.Set("output", opts.Output)
viper.Set("overwrite", opts.Overwrite)
viper.Set("skip-check", opts.SkipCTFDCheck)
viper.Set("unsolved", opts.UnsolvedOnly)
viper.Set("watch", opts.Watch)
viper.Set("watch-interval", opts.WatchInterval)
err := viper.SafeWriteConfigAs(path.Join(opts.Output, ".ctftool.yaml"))

if opts.URL != "" {
viper.Set("url", opts.URL)
}
if opts.Username != "" {
viper.Set("username", opts.Username)
}
if opts.Password != "" {
viper.Set("password", opts.Password)
}
if opts.Token != "" {
viper.Set("token", opts.Token)
}
if opts.Output != "" {
viper.Set("output", opts.Output)
}
if opts.Overwrite {
viper.Set("overwrite", opts.Overwrite)
}
if opts.SkipCTFDCheck {
viper.Set("skip-check", opts.SkipCTFDCheck)
}
if opts.UnsolvedOnly {
viper.Set("unsolved", opts.UnsolvedOnly)
}
if opts.Watch {
viper.Set("watch", opts.Watch)
}
if opts.WatchInterval != 0 {
viper.Set("watch-interval", opts.WatchInterval)
}
if opts.MaxFileSize != 0 {
viper.Set("max-file-size", opts.MaxFileSize)
}
if options.RateLimit != 0 {
viper.Set("rate-limit", options.RateLimit)
}

err := viper.WriteConfigAs(path.Join(opts.Output, ".ctftool.yaml"))
CheckErr(err)

log.WithField("file", path.Join(opts.Output, ".ctftool.yaml")).Info("Saved config file")
log.Info("You can now run `ctftool` from the same directory without specifying the --url, --username and --password in that directory")
log.Info("You can now run ctftool without any arguments")
}

// sort challenges modifies the order of the challenges slice
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ Flags:
CombinedFlagUsages: combinedFlagUsages,
})
})

err := viper.BindPFlags(rootCmd.PersistentFlags())
CheckErr(err)
}

// initConfig reads in config file and ENV variables if set.
Expand Down

0 comments on commit 7243234

Please sign in to comment.