Skip to content

Commit

Permalink
Rename Config to Cli
Browse files Browse the repository at this point in the history
I was thrown when I wanted to output attributes, since the flag was
`--output-attributes`, when I assumed it would just be `--atributes`.

Upon further investigation it seems that `Configs` really is not a good
name for what it's doing, because it only really contains CLI stuff, so
I have renamed it accordingly.
  • Loading branch information
surminus committed Jun 16, 2024
1 parent dd34cf6 commit ffbe9da
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 49 deletions.
40 changes: 20 additions & 20 deletions config.go → cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import (
flag "github.com/spf13/pflag"
)

// Configs sets different settings when running something with viaduct
type Configs struct {
DryRun bool
DumpManifest bool
OutputAttributes bool
Quiet bool
Silent bool
// CliFlags configures the command-line toolset
type CliFlags struct {
Attributes bool
DryRun bool
DumpManifest bool
Quiet bool
Silent bool
}

// initConfigs loads configuration
func initConfigs(c *Configs) {
// initCli loads command-line options
func initCli(c *CliFlags) {
var (
dryRun bool
dumpManifest bool
outputAttributes bool
quiet bool
silent bool
attributes bool
dryRun bool
dumpManifest bool
quiet bool
silent bool
)

flag.BoolVar(&dryRun, "dry-run", false, "Test changes with dry-run mode")
flag.BoolVar(&outputAttributes, "output-attributes", false, "Output attributes only")
flag.BoolVar(&attributes, "attributes", false, "Display known attributes")
flag.BoolVar(&dumpManifest, "dump-manifest", false, "Dump the full manifest after the run")
flag.BoolVar(&quiet, "quiet", false, "Quiet mode will only display errors during a run")
flag.BoolVar(&silent, "silent", false, "Silent mode will suppress all output")
Expand All @@ -36,29 +36,29 @@ func initConfigs(c *Configs) {
log.Fatal("Cannot use --silent and --quiet together")
}

c.Attributes = attributes
c.DryRun = dryRun
c.DumpManifest = dumpManifest
c.OutputAttributes = outputAttributes
c.Quiet = quiet
c.Silent = silent
}

// SetDryRun enables dry run mode.
func (c *Configs) SetDryRun() {
func (c *CliFlags) SetDryRun() {
c.DryRun = true
}

// SetDumpManifest enables dumping the manifest.
func (c *Configs) SetDumpManifest() {
func (c *CliFlags) SetDumpManifest() {
c.DumpManifest = true
}

// SetQuiet enables quiet mode.
func (c *Configs) SetQuiet() {
func (c *CliFlags) SetQuiet() {
c.Quiet = true
}

// SetSilent enables silent mode.
func (c *Configs) SetSilent() {
func (c *CliFlags) SetSilent() {
c.Silent = true
}
4 changes: 2 additions & 2 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ func CommandTrue(command string) bool {

// nolint:gosec
cmd := exec.Command("bash", "-c", strings.Join(c, " "))
if Config.Quiet {
if Cli.Quiet {
cmd.Stderr = os.Stderr
} else if !Config.Silent {
} else if !Cli.Silent {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
Expand Down
4 changes: 2 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func Log(v ...interface{}) {
}

func NewLogger(resource, action string) *Logger {
if Config.Silent {
if Cli.Silent {
return NewSilentLogger()
}

if Config.Quiet {
if Cli.Quiet {
return NewQuietLogger(resource, action)
}

Expand Down
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (

var (
Attribute SystemAttributes
Config Configs
Cli CliFlags

tmpDirPath string
)

func init() {
initAttributes(&Attribute)
initConfigs(&Config)
initCli(&Cli)

if Config.OutputAttributes {
if Cli.Attributes {
fmt.Println(Attribute.JSON())
os.Exit(0)
}

if Config.DryRun {
if Cli.DryRun {
log.Println("WARNING: dry run mode enabled")
}
}
4 changes: 2 additions & 2 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (m *Manifest) Run() {
}
}

if Config.DumpManifest {
if Cli.DumpManifest {
tmpName := fmt.Sprintf("/tmp/viaduct-%d.json", time.Now().Unix())

out, err := json.MarshalIndent(m.resources, "", " ")
Expand All @@ -207,7 +207,7 @@ func (m *Manifest) Run() {
}

if withErrors {
if !Config.DumpManifest {
if !Cli.DumpManifest {
log.Info("To see all resources, run with --dump-manifest")
}
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions resources/apt.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (a *Apt) Run(log *viaduct.Logger) error {
// AptUpdate is a helper function to perform "apt-get update"
// Should be converted to a proper resource
func (a *Apt) updateApt(log *viaduct.Logger) error {
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info()
return nil
}
Expand All @@ -144,7 +144,7 @@ func (a *Apt) updateApt(log *viaduct.Logger) error {

// Create adds a new apt repository
func (a *Apt) createApt(log *viaduct.Logger) error {
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(a.Name)
return nil
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func (a *Apt) signingKeyPath() string {

// Delete removes an apt repository
func (a *Apt) deleteApt(log *viaduct.Logger) error {
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(a.Name)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions resources/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (d *Directory) Run(log *viaduct.Logger) error {
func (d *Directory) createDirectory(log *viaduct.Logger) error {
path := viaduct.ExpandPath(d.Path)

if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(d.Path)
return nil
}
Expand All @@ -82,7 +82,7 @@ func (d *Directory) createDirectory(log *viaduct.Logger) error {

// Delete deletes a directory.
func (d *Directory) deleteDirectory(log *viaduct.Logger) error {
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(d.Path)
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion resources/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (a *Download) get(log *viaduct.Logger) error {
path := viaduct.ExpandPath(a.Path)
logmsg := fmt.Sprintf("%s -> %s", a.URL, path)

if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(logmsg)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions resources/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (e *Execute) runExecute(log *viaduct.Logger) error {
}

log.Info(e.Command, " -> started")
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
return nil
}

Expand All @@ -93,13 +93,13 @@ func (e *Execute) runExecute(log *viaduct.Logger) error {
}

func setCommandOutput(cmd *exec.Cmd) {
if viaduct.Config.Silent {
if viaduct.Cli.Silent {
cmd.Stdout = nil
cmd.Stderr = nil
return
}

if viaduct.Config.Quiet {
if viaduct.Cli.Quiet {
cmd.Stdout = nil
cmd.Stderr = os.Stderr
return
Expand Down
4 changes: 2 additions & 2 deletions resources/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (f *File) Run(log *viaduct.Logger) error {

// Create creates or updates a file
func (f *File) createFile(log *viaduct.Logger) error {
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(f.Path)
return nil
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func (f *File) createFile(log *viaduct.Logger) error {

// Delete deletes a file
func (f *File) deleteFile(log *viaduct.Logger) error {
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(f.Path)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions resources/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (g *Git) createGit(log *viaduct.Logger) error {
path := viaduct.ExpandPath(g.Path)
logmsg := fmt.Sprintf("%s -> %s", g.URL, path)

if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(logmsg)
return nil
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (g *Git) createGit(log *viaduct.Logger) error {
if !viaduct.FileExists(path) {
progress := os.Stdout

if viaduct.Config.Quiet || viaduct.Config.Silent {
if viaduct.Cli.Quiet || viaduct.Cli.Silent {
devnull, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0755)
if err != nil {
return err
Expand Down Expand Up @@ -158,7 +158,7 @@ func (g *Git) createGit(log *viaduct.Logger) error {
func (g *Git) deleteGit(log *viaduct.Logger) error {
path := viaduct.ExpandPath(g.Path)

if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(path)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions resources/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (l *Link) createLink(log *viaduct.Logger) error {
path := viaduct.ExpandPath(l.Path)
logmsg := fmt.Sprintf("%s -> %s", source, path)

if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(logmsg)
return nil
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func (l *Link) createLink(log *viaduct.Logger) error {
func (l *Link) deleteLink(log *viaduct.Logger) error {
path := viaduct.ExpandPath(l.Path)

if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
log.Info(path)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions resources/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (p *Package) Run(log *viaduct.Logger) error {

func (p *Package) install(log *viaduct.Logger) error {
log.Info("Packages:\n\t", strings.Join(p.Names, "\n\t"))
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
return nil
}

Expand All @@ -82,7 +82,7 @@ func (p *Package) install(log *viaduct.Logger) error {

func (p *Package) uninstall(log *viaduct.Logger) error {
log.Info("Packages:\n\t", strings.Join(p.Names, "\n\t"))
if viaduct.Config.DryRun {
if viaduct.Cli.DryRun {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import "github.com/surminus/viaduct"
var testLogger *viaduct.Logger

func init() {
viaduct.Config.SetSilent()
viaduct.Cli.SetSilent()
testLogger = viaduct.NewLogger("Test", "Testing")
}

0 comments on commit ffbe9da

Please sign in to comment.