/
main.go
233 lines (212 loc) · 5.81 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"time"
"github.com/mashiike/ichigeki"
"github.com/mashiike/ichigeki/s3log"
"github.com/pelletier/go-toml"
)
const (
Version = "current"
defaultConfigPath = ".config/ichigeki/default.toml"
)
func main() {
flag.CommandLine.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "ichigeki [options] -- (commands)")
fmt.Fprintln(flag.CommandLine.Output(), "version:", Version)
flag.CommandLine.PrintDefaults()
}
cfg, err := defaultConfig()
if err != nil {
log.Fatal("[error] ", err)
}
cfg.SetFlags(flag.CommandLine)
flag.Parse()
if err := cfg.Restrict(); err != nil {
log.Fatal("[error] ", err)
}
var args []string
if flag.Arg(0) == "--" {
args = flag.Args()[1:]
} else {
args = flag.Args()
}
if len(args) == 0 {
flag.CommandLine.Usage()
log.Fatal("[error] commands not found")
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
ld, err := cfg.LogDestination(ctx)
if err != nil {
log.Fatal("[error] ", err)
}
h := &ichigeki.Hissatsu{
Args: args,
Name: cfg.Name,
DefaultNameTemplate: cfg.DefaultNameTemplate,
LogDestination: ld,
ConfirmDialog: cfg.ConfirmDialog,
ExecDate: cfg.ExecDate,
Script: func(ctx ichigeki.Context, stdout io.Writer, stderr io.Writer) error {
env := os.Environ()
env = append(env, `ICHIGEKI_EXECUTION_ENV=ichigeki `+Version)
env = append(env, `ICHIGEKI_EXECUTION_NAME=`+ctx.Name)
env = append(env, `ICHIGEKI_EXECUTION_DATE=`+ctx.ExecDate)
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Env = env
if err := cmd.Run(); err != nil {
return fmt.Errorf("command runtime error: %w", err)
}
return nil
},
}
if err := h.ExecuteWithContext(ctx); err != nil {
log.Fatal("[error] ", err)
}
}
type config struct {
Name string `toml:"-"`
ConfirmDialog *bool `toml:"confirm_dialog"`
DefaultNameTemplate string `toml:"default_name_template"`
File *fileConfig `toml:"file"`
S3 *s3Config `toml:"s3"`
ExecDate time.Time `toml:"-"`
optDir string `toml:"-"`
optName string `toml:"-"`
optS3URLPrefix string `toml:"-"`
optNoConfirmDialog bool `toml:"-"`
optExecDate string `toml:"-"`
}
type s3Config struct {
Bucket string `toml:"bucket"`
ObjectPrefix string `toml:"object_prefix"`
}
type fileConfig struct {
Dir string `toml:"dir"`
LogFilePostfix string `toml:"log_file_postfix"`
}
func loadConfig(path string) (*config, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
}
defer fp.Close()
cfg := &config{}
decoder := toml.NewDecoder(fp)
if err := decoder.Decode(cfg); err != nil {
return nil, err
}
return cfg, nil
}
func configExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func defaultConfig() (*config, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("can not get user config dir: %w", err)
}
defaultConfigPath := filepath.Join(homeDir, defaultConfigPath)
if configExists(defaultConfigPath) {
if cfg, err := loadConfig(defaultConfigPath); err != nil {
return nil, fmt.Errorf("default config load failed: %w", err)
} else {
return cfg, nil
}
}
return &config{}, nil
}
func (cfg *config) SetFlags(fs *flag.FlagSet) {
flag.StringVar(&cfg.optDir, "dir", "", "log destination for s3")
flag.StringVar(&cfg.optName, "name", "", "ichigeki name")
flag.StringVar(&cfg.optS3URLPrefix, "s3-url-prefix", "", "log destination for s3")
flag.StringVar(&cfg.optExecDate, "exec-date", "", "scheduled execution date")
flag.BoolVar(&cfg.optNoConfirmDialog, "no-confirm-dialog", false, "do confirm")
}
func (cfg *config) Restrict() error {
cfg.Name = cfg.optName
if cfg.optNoConfirmDialog {
cfg.ConfirmDialog = ichigeki.Bool(false)
}
if cfg.optS3URLPrefix != "" {
u, err := url.Parse(cfg.optS3URLPrefix)
if err != nil {
return fmt.Errorf("s3-url-prefix can not parse: %w", err)
}
if u.Scheme != "s3" {
return fmt.Errorf("s3-url-prefix is not s3 url format")
}
if cfg.S3 == nil {
cfg.S3 = &s3Config{}
}
cfg.S3.Bucket = u.Host
cfg.S3.ObjectPrefix = u.Path
}
if cfg.optDir != "" {
if !filepath.IsAbs(cfg.optDir) {
var err error
cfg.optDir, err = filepath.Abs(cfg.optDir)
if err != nil {
return fmt.Errorf("can not convert to abs path: %w", err)
}
if cfg.File == nil {
cfg.File = &fileConfig{}
}
cfg.File.Dir = cfg.optDir
}
}
if cfg.optExecDate != "" {
t, err := time.Parse("2006-01-02", cfg.optExecDate)
if err != nil {
return fmt.Errorf("exec date parse failed: %w", err)
}
cfg.ExecDate = t
}
return nil
}
func (cfg *config) LogDestination(ctx context.Context) (ichigeki.LogDestination, error) {
logDestinations := make([]ichigeki.LogDestination, 0, 2)
if cfg.S3 != nil && cfg.S3.Bucket != "" {
ld, err := s3log.New(ctx, &s3log.Config{
Bucket: cfg.S3.Bucket,
ObjectPrefix: cfg.S3.ObjectPrefix,
})
if err != nil {
return nil, fmt.Errorf("s3 log destination: %w", err)
}
logDestinations = append(logDestinations, ld)
}
if cfg.File != nil && cfg.File.Dir != "" {
logDestinations = append(logDestinations, &ichigeki.LocalFile{
Path: cfg.File.Dir,
LogFilePostfix: cfg.File.LogFilePostfix,
})
}
if len(logDestinations) == 0 {
wd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("can not get working directory: %w", err)
}
logDestinations = append(logDestinations, &ichigeki.LocalFile{
Path: wd,
})
}
if len(logDestinations) == 1 {
return logDestinations[0], nil
}
return ichigeki.MultipleLogDestination(logDestinations), nil
}