File: cmd.go

package info (click to toggle)
addchain 0.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,396 kB
  • sloc: sh: 428; makefile: 10
file content (50 lines) | stat: -rw-r--r-- 1,304 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package cli

import (
	"flag"
	"io"
	"log"
	"os"

	"github.com/google/subcommands"
)

// Command is a base for all subcommands.
type Command struct {
	Log *log.Logger
}

// NewBaseCommand builds a new base command for the named tool.
func NewBaseCommand(name string) Command {
	return Command{
		Log: log.New(os.Stderr, name+": ", 0),
	}
}

// SetFlags is a stub implementation of the SetFlags methods that does nothing.
func (Command) SetFlags(f *flag.FlagSet) {}

// UsageError logs a usage error and returns a suitable exit code.
func (c Command) UsageError(format string, args ...interface{}) subcommands.ExitStatus {
	c.Log.Printf(format, args...)
	return subcommands.ExitUsageError
}

// Fail logs an error message and returns a failing exit code.
func (c Command) Fail(format string, args ...interface{}) subcommands.ExitStatus {
	c.Log.Printf(format, args...)
	return subcommands.ExitFailure
}

// Error logs err and returns a failing exit code.
func (c Command) Error(err error) subcommands.ExitStatus {
	return c.Fail(err.Error())
}

// CheckClose closes cl. On error it logs and writes to the status pointer.
// Intended for deferred Close() calls.
func (c Command) CheckClose(statusp *subcommands.ExitStatus, cl io.Closer) {
	if err := cl.Close(); err != nil {
		*statusp = c.Error(err)
	}
}