-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command to generate template configuration file
- Loading branch information
Leonid Zaikin
committed
Mar 18, 2024
1 parent
73f0195
commit 692c2d3
Showing
3 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package configcmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/aztekas/cleura-client-go/cmd/cleura/utils" | ||
"github.com/urfave/cli/v2" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func generateConfigTemplateCommand() *cli.Command { | ||
return &cli.Command{ | ||
Name: "generate-template", | ||
Description: "Generate configuration file template on the given path", | ||
Usage: "Generate configuration file template on the given path", | ||
Flags: []cli.Flag{ | ||
&cli.StringFlag{ | ||
Name: "output-file", | ||
Aliases: []string{"o"}, | ||
Usage: "Path to configuration file. $HOME/.config/cleura/config if not set. NB: Overwrites existing if found", | ||
}, | ||
}, | ||
Action: func(ctx *cli.Context) error { | ||
config := &Config{ | ||
ActiveConfig: "default", | ||
Configurations: map[string]Configuration{ | ||
"default": {}, | ||
}, | ||
} | ||
templateByte, err := yaml.Marshal(config) | ||
if err != nil { | ||
return err | ||
} | ||
path, err := utils.ChoosePath(ctx.String("output-file")) | ||
if err != nil { | ||
return err | ||
} | ||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
if _, err := f.Write(templateByte); err != nil { | ||
f.Close() // ignore error; Write error takes precedence | ||
return err | ||
} | ||
if err := f.Close(); err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters