Skip to content

Commit

Permalink
Add second verbosity level and update related logic
Browse files Browse the repository at this point in the history
In this update, a second verbosity level (Verbose2) has been added to the codebase. The normal -v (Verbose) is not reporting the content of the script anymore but -vv (Verbose2) only. Additionally, the verbosity short flag in cmd/spot/main.go has been modified and its type to boolean slice to allow increased verbosity when multiple "v" flags are used.
  • Loading branch information
umputun committed Apr 7, 2024
1 parent 9380bcf commit 4f177c1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 14 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ linters:
- revive
- govet
- unconvert
- megacheck
- gas
- dupl
- misspell
Expand Down
27 changes: 18 additions & 9 deletions cmd/spot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -58,12 +59,12 @@ type options struct {
GenTemplate string `long:"gen.template" description:"template file" default:"json"`
GenOutput string `long:"gen.output" description:"output file" default:"stdout"`

Version bool `long:"version" description:"show version"`
Version bool `short:"V" long:"version" description:"show version"`

NoColor bool `long:"no-color" env:"SPOT_NO_COLOR" description:"disable color output"`
Dry bool `long:"dry" description:"dry run"`
Verbose bool `short:"v" long:"verbose" description:"verbose mode"`
Dbg bool `long:"dbg" description:"debug mode"`
NoColor bool `long:"no-color" env:"SPOT_NO_COLOR" description:"disable color output"`
Dry bool `long:"dry" description:"dry run"`
Verbose []bool `short:"v" long:"verbosity" description:"Verbosity level"`
Dbg bool `long:"dbg" description:"debug mode"`
}

// SecretsProvider defines secrets provider options, for all supported providers
Expand Down Expand Up @@ -97,12 +98,19 @@ func main() {
var opts options
p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
if _, err := p.Parse(); err != nil {
os.Exit(1)
if !errors.Is(err.(*flags.Error).Type, flags.ErrHelp) {
// cli error, not help
fmt.Printf("%v", err)
os.Exit(1)
}
os.Exit(0) // help printed
}

if opts.Version {
fmt.Printf("spot %s\n", revision)
os.Exit(0) // already printed
os.Exit(0)
}

setupLog(opts.Dbg) // set initial log, will be updated later with secrets

if !opts.GenEnable || opts.GenOutput != "stdout" {
Expand Down Expand Up @@ -320,7 +328,7 @@ func makeRunner(opts options, pbook *config.PlayBook) (*runner.Process, error) {
if err != nil {
return nil, fmt.Errorf("can't get ssh key: %w", err)
}
logs := executor.MakeLogs(opts.Verbose, opts.NoColor, pbook.AllSecretValues())
logs := executor.MakeLogs(len(opts.Verbose) > 0, opts.NoColor, pbook.AllSecretValues())
connector, err := executor.NewConnector(sshKey, opts.SSHTimeout, logs)
if err != nil {
return nil, fmt.Errorf("can't create connector: %w", err)
Expand All @@ -336,7 +344,8 @@ func makeRunner(opts options, pbook *config.PlayBook) (*runner.Process, error) {
Only: opts.Only,
Skip: opts.Skip,
Logs: logs,
Verbose: opts.Verbose,
Verbose: len(opts.Verbose) > 0,
Verbose2: len(opts.Verbose) > 1,
Dry: opts.Dry,
SSHShell: opts.SSHShell,
}
Expand Down
1 change: 1 addition & 0 deletions pkg/runner/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type execCmd struct {
tsk *config.Task
exec executor.Interface
verbose bool
verbose2 bool
sshShell string
onExit string
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Process struct {
Playbook Playbook
Logs executor.Logs
Verbose bool
Verbose2 bool
Dry bool
SSHShell string

Expand Down Expand Up @@ -204,7 +205,7 @@ func (p *Process) runTaskOnHost(ctx context.Context, tsk *config.Task, hostAddr,
stCmd := time.Now()

ec := execCmd{cmd: cmd, hostAddr: hostAddr, hostName: hostName, tsk: &activeTask, exec: remote,
verbose: p.Verbose, sshShell: p.SSHShell, onExit: cmd.OnExit}
verbose: p.Verbose, verbose2: p.Verbose2, sshShell: p.SSHShell, onExit: cmd.OnExit}
ec = p.pickCmdExecutor(cmd, ec, hostAddr, hostName) // pick executor on dry run or local command

repHostAddr, repHostName := ec.hostAddr, ec.hostName
Expand Down Expand Up @@ -232,7 +233,7 @@ func (p *Process) runTaskOnHost(ctx context.Context, tsk *config.Task, hostAddr,

p.updateVars(exResp.vars, cmd, &activeTask) // set variables from command output to all commands env in task

if exResp.verbose != "" && ec.verbose {
if exResp.verbose != "" && ec.verbose2 {
report(repHostAddr, repHostName, exResp.verbose)
}

Expand All @@ -253,7 +254,8 @@ func (p *Process) runTaskOnHost(ctx context.Context, tsk *config.Task, hostAddr,
if p.anyRemoteCommand(&activeTask) {
report(hostAddr, hostName, "completed task %q, commands: %d (%v)\n", activeTask.Name, count, since(stTask))
} else {
report("localhost", "", "completed task %q, commands: %d (%v)\n", activeTask.Name, count, since(stTask))
report("localhost", "", "completed task %q, commands: %d (%v)\n",
activeTask.Name, count, since(stTask))
}

return count, tskVars, nil
Expand Down
34 changes: 33 additions & 1 deletion pkg/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func TestProcess_RunVerbose(t *testing.T) {
require.NoError(t, err)
})

t.Run("multi-line script with verbose", func(t *testing.T) {
t.Run("multi-line script with verbose (1)", func(t *testing.T) {
log.SetOutput(io.Discard)
stdout := captureStdOut(t, func() {
logs := executor.MakeLogs(true, false, nil)
Expand All @@ -599,6 +599,37 @@ func TestProcess_RunVerbose(t *testing.T) {
Logs: logs,
Only: []string{"copy configuration", "some command"},
Verbose: true,
Verbose2: false,
}

_, err = p.Run(ctx, "task1", testingHostAndPort)
require.NoError(t, err)
})

t.Log(stdout)
assert.NotContains(t, stdout, `+ #!/bin/sh`)
assert.NotContains(t, stdout, `+ du -hcs /srv`)
assert.Contains(t, stdout, "> - name: wait\n")
})

t.Run("multi-line script with verbose (2)", func(t *testing.T) {
log.SetOutput(io.Discard)
stdout := captureStdOut(t, func() {
logs := executor.MakeLogs(true, false, nil)
connector, err := executor.NewConnector("testdata/test_ssh_key", time.Second*10, logs)
require.NoError(t, err)

conf, err := config.New("testdata/conf.yml", nil, nil)
require.NoError(t, err)

p := Process{
Concurrency: 1,
Connector: connector,
Playbook: conf,
Logs: logs,
Only: []string{"copy configuration", "some command"},
Verbose: true,
Verbose2: true,
}

_, err = p.Run(ctx, "task1", testingHostAndPort)
Expand All @@ -608,6 +639,7 @@ func TestProcess_RunVerbose(t *testing.T) {
t.Log(stdout)
assert.Contains(t, stdout, `+ #!/bin/sh`)
assert.Contains(t, stdout, `+ du -hcs /srv`)
assert.Contains(t, stdout, "> - name: wait\n")
})
}

Expand Down

0 comments on commit 4f177c1

Please sign in to comment.