Skip to content

Commit b928b35

Browse files
committed
use new cli package
Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
1 parent 0bbf544 commit b928b35

File tree

805 files changed

+468
-148096
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

805 files changed

+468
-148096
lines changed

Gopkg.lock

Lines changed: 20 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,18 @@ CPU % MEM USAGE / LIMIT MEM % NET I/O
4242
```
4343

4444
```console
45-
$ magneto --help
46-
_
47-
_ __ ___ __ _ __ _ _ __ ___| |_ ___
48-
| '_ ` _ \ / _` |/ _` | '_ \ / _ \ __/ _ \
49-
| | | | | | (_| | (_| | | | | __/ || (_) |
50-
|_| |_| |_|\__,_|\__, |_| |_|\___|\__\___/
51-
|___/
52-
53-
Pipe runc events to a stats TUI (Text User Interface).
54-
Version: v0.2.1
55-
Build: 30036e2
56-
57-
-d run in debug mode
58-
-v print version and exit (shorthand)
59-
-version
60-
print version and exit
45+
$ magneto -h
46+
magneto - Pipe runc events to a stats TUI (Text User Interface).
47+
48+
Usage: magneto <command>
49+
50+
Flags:
51+
52+
-d enable debug logging (default: false)
53+
54+
Commands:
55+
56+
version Show the version information.
6157
```
6258

6359
**NOTE:** Almost all this is the exact same as `docker stats`, so thanks to

main.go

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,36 @@ package main
22

33
import (
44
"bufio"
5+
"context"
56
"encoding/json"
67
"flag"
78
"fmt"
89
"io"
910
"os"
11+
"os/signal"
1012
"strconv"
1113
"strings"
1214
"sync"
15+
"syscall"
1316
"text/tabwriter"
1417
"time"
1518

1619
units "github.com/docker/go-units"
1720
"github.com/genuinetools/magneto/types"
1821
"github.com/genuinetools/magneto/version"
22+
"github.com/genuinetools/pkg/cli"
1923
"github.com/opencontainers/runc/libcontainer/system"
2024
"github.com/sirupsen/logrus"
2125
)
2226

2327
const (
24-
// BANNER is what is printed for help/info output
25-
BANNER = ` _
26-
_ __ ___ __ _ __ _ _ __ ___| |_ ___
27-
| '_ ` + "`" + ` _ \ / _` + "`" + ` |/ _` + "`" + ` | '_ \ / _ \ __/ _ \
28-
| | | | | | (_| | (_| | | | | __/ || (_) |
29-
|_| |_| |_|\__,_|\__, |_| |_|\___|\__\___/
30-
|___/
31-
32-
Pipe runc events to a stats TUI (Text User Interface).
33-
Version: %s
34-
Build: %s
35-
36-
`
37-
3828
nanoSecondsPerSecond = 1e9
3929
)
4030

4131
var (
4232
debug bool
43-
vrsn bool
4433
)
4534

46-
func init() {
47-
// Parse flags
48-
flag.BoolVar(&vrsn, "version", false, "print version and exit")
49-
flag.BoolVar(&vrsn, "v", false, "print version and exit (shorthand)")
50-
flag.BoolVar(&debug, "d", false, "run in debug mode")
51-
52-
flag.Usage = func() {
53-
fmt.Fprint(os.Stderr, fmt.Sprintf(BANNER, version.VERSION, version.GITCOMMIT))
54-
flag.PrintDefaults()
55-
}
56-
57-
flag.Parse()
58-
59-
if vrsn {
60-
fmt.Printf("magneto version %s, build %s", version.VERSION, version.GITCOMMIT)
61-
os.Exit(0)
62-
}
63-
64-
// Set log level
65-
if debug {
66-
logrus.SetLevel(logrus.DebugLevel)
67-
}
68-
}
69-
7035
type event struct {
7136
Type string `json:"type"`
7237
ID string `json:"id"`
@@ -90,29 +55,70 @@ type containerStats struct {
9055
}
9156

9257
func main() {
93-
// create the writer
94-
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
95-
printHeader := func() {
96-
fmt.Fprint(os.Stdout, "\033[2J")
97-
fmt.Fprint(os.Stdout, "\033[H")
98-
io.WriteString(w, "CPU %\tMEM USAGE / LIMIT\tMEM %\tNET I/O\tBLOCK I/O\tPIDS\n")
99-
}
58+
// Create a new cli program.
59+
p := cli.NewProgram()
60+
p.Name = "magneto"
61+
p.Description = "Pipe runc events to a stats TUI (Text User Interface)"
62+
63+
// Set the GitCommit and Version.
64+
p.GitCommit = version.GITCOMMIT
65+
p.Version = version.VERSION
66+
67+
// Setup the global flags.
68+
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
69+
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
70+
71+
// Set the before function.
72+
p.Before = func(ctx context.Context) error {
73+
// Set the log level.
74+
if debug {
75+
logrus.SetLevel(logrus.DebugLevel)
76+
}
10077

101-
// collect the stats
102-
s := &containerStats{
103-
clockTicksPerSecond: uint64(system.GetClockTicks()),
104-
bufReader: bufio.NewReaderSize(nil, 128),
78+
return nil
10579
}
10680

107-
go s.collect()
81+
// Set the main program action.
82+
p.Action = func(ctx context.Context, args []string) error {
83+
// On ^C, or SIGTERM handle exit.
84+
c := make(chan os.Signal, 1)
85+
signal.Notify(c, os.Interrupt)
86+
signal.Notify(c, syscall.SIGTERM)
87+
go func() {
88+
for sig := range c {
89+
logrus.Infof("Received %s, exiting.", sig.String())
90+
os.Exit(0)
91+
}
92+
}()
93+
// create the writer
94+
w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
95+
printHeader := func() {
96+
fmt.Fprint(os.Stdout, "\033[2J")
97+
fmt.Fprint(os.Stdout, "\033[H")
98+
io.WriteString(w, "CPU %\tMEM USAGE / LIMIT\tMEM %\tNET I/O\tBLOCK I/O\tPIDS\n")
99+
}
108100

109-
for range time.Tick(5 * time.Second) {
110-
printHeader()
111-
if err := s.Display(w); err != nil {
112-
logrus.Error(err)
101+
// collect the stats
102+
s := &containerStats{
103+
clockTicksPerSecond: uint64(system.GetClockTicks()),
104+
bufReader: bufio.NewReaderSize(nil, 128),
113105
}
114-
w.Flush()
106+
107+
go s.collect()
108+
109+
for range time.Tick(5 * time.Second) {
110+
printHeader()
111+
if err := s.Display(w); err != nil {
112+
logrus.Error(err)
113+
}
114+
w.Flush()
115+
}
116+
117+
return nil
115118
}
119+
120+
// Run our program.
121+
p.Run()
116122
}
117123

118124
func (s *containerStats) collect() {

vendor/github.com/docker/go-units/CONTRIBUTING.md

Lines changed: 0 additions & 67 deletions
This file was deleted.

vendor/github.com/docker/go-units/MAINTAINERS

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)