Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
fix --conv-yaml func
Browse files Browse the repository at this point in the history
  • Loading branch information
boypt committed Aug 4, 2021
1 parent 94e222a commit 7d8ff47
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
10 changes: 7 additions & 3 deletions engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ func InitConf(specPath string) (*Config, error) {
cf := viper.ConfigFileUsed()
log.Println("[config] selected config file: ", cf)
if !configExists || dirChanged {
c.WriteYaml()
log.Println("[config] config file written: ", cf, "exists:", configExists, "dirchanged", dirChanged)
c.WriteDefault()
log.Println("[config] config file updated: ", cf, "exists:", configExists, "dirchanged", dirChanged)
}

return c, nil
Expand Down Expand Up @@ -221,8 +221,12 @@ func (c *Config) SyncViper(nc Config) {
}
}

func (c *Config) WriteYaml() error {
func (c *Config) WriteDefault() error {
cf := viper.ConfigFileUsed()
return c.WriteYaml(cf)
}

func (c *Config) WriteYaml(cf string) error {
d, err := yaml.Marshal(c)
if err != nil {
return err
Expand Down
16 changes: 10 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@ func (s *Server) Run(version string) error {
// write cloud-torrent.yaml at the same dir with -c conf and exit
if s.ConvYAML {
cf := viper.ConfigFileUsed()
log.Println("[config] current file path: ", cf)
// replace orig config file ext with ".yaml"
ymlcf := cf[:len(cf)-len(path.Ext(cf))] + ".yaml"
if err := viper.WriteConfigAs(ymlcf); err != nil {
return err
if !strings.HasSuffix(cf, ".yaml") {
log.Println("[config] current file path: ", cf)

// replace orig config file ext with ".yaml"
ymlcf := cf[:len(cf)-len(path.Ext(cf))] + ".yaml"
if err := c.WriteYaml(ymlcf); err != nil {
return err
}
return fmt.Errorf("config file converted and written to: %s", ymlcf)
}
return fmt.Errorf("ERROR: Config file converted and written to: %s", ymlcf)
return fmt.Errorf("config file is already yaml: %s", cf)
}

if err := detectDiskStat(c.DownloadDirectory); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion server/server_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (s *Server) apiConfigure(data []byte) error {
// now it's safe to save the configure
s.engineConfig.SyncViper(c)
s.engineConfig = &c
if err := s.engineConfig.WriteYaml(); err != nil {
if err := s.engineConfig.WriteDefault(); err != nil {
return err
}
log.Printf("[api] config saved")
Expand Down

0 comments on commit 7d8ff47

Please sign in to comment.