forked from Songmu/ecschedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ecsched.go
81 lines (76 loc) · 1.77 KB
/
ecsched.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ecschedule
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/aws/aws-sdk-go/aws/session"
)
const cmdName = "ecschedule"
// Run the ecschedule
func Run(ctx context.Context, argv []string, outStream, errStream io.Writer) error {
log.SetOutput(errStream)
log.SetPrefix(fmt.Sprintf("[%s] ", cmdName))
nameAndVer := fmt.Sprintf("%s (v%s rev:%s)", cmdName, version, revision)
fs := flag.NewFlagSet(
fmt.Sprintf("%s (v%s rev:%s)", cmdName, version, revision), flag.ContinueOnError)
fs.SetOutput(errStream)
fs.Usage = func() {
fmt.Fprintf(fs.Output(), "Usage of %s:\n", nameAndVer)
fs.PrintDefaults()
fmt.Fprintf(fs.Output(), "\nCommands:\n")
formatCommands(fs.Output())
}
var (
conf = fs.String("conf", "", "configuration")
ver = fs.Bool("version", false, "display version")
)
if err := fs.Parse(argv); err != nil {
return err
}
if *ver {
return printVersion(outStream)
}
sess, err := session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
return err
}
accountID, err := GetAWSAccountID(sess)
if err != nil {
return err
}
a := &app{
AccountID: accountID,
Session: sess,
}
if *conf != "" {
f, err := os.Open(*conf)
if err != nil {
return err
}
defer f.Close()
c, err := LoadConfig(ctx, f, a.AccountID, *conf)
if err != nil {
return err
}
a.Config = c
}
ctx = setApp(ctx, a)
argv = fs.Args()
if len(argv) < 1 {
return fmt.Errorf("no subcommand specified")
}
rnr, ok := cmder.dispatch[argv[0]]
if !ok {
return fmt.Errorf("unknown subcommand: %s", argv[0])
}
return rnr.Run(ctx, argv[1:], outStream, errStream)
}
func printVersion(out io.Writer) error {
_, err := fmt.Fprintf(out, "%s v%s (rev:%s)\n", cmdName, version, revision)
return err
}