feat: add safer CLI cleanup modes#479
Conversation
Super-linter summary
Super-linter detected linting errors For more information, see the GitHub Actions workflow run Powered by Super-linter GO_MODULESJSCPD |
Super-linter summary
Super-linter detected linting errors For more information, see the GitHub Actions workflow run Powered by Super-linter GO_MODULESJSCPD |
Super-linter summary
All files and directories linted successfully For more information, see the GitHub Actions workflow run Powered by Super-linter |
| return strings.Join(*f, ",") | ||
| } | ||
|
|
||
| func (f *stringSliceFlag) Set(value string) error { |
| fmt.Fprintln(os.Stderr, err) | ||
| fmt.Fprintln(os.Stderr) | ||
| fmt.Fprint(os.Stderr, usage()) |
| } | ||
|
|
||
| if err := run(cfg); err != nil { | ||
| fmt.Fprintln(os.Stderr, err) |
| return false, errors.Wrap(diags, "parse terraform file") | ||
| } | ||
|
|
||
| ret := tfrbac(file.Body()).Bytes() |
There was a problem hiding this comment.
これより前に file のnilチェックを入れた方が良さそうです。
| } | ||
|
|
||
| if showVersion { | ||
| fmt.Fprintln(os.Stdout, version) |
There was a problem hiding this comment.
| fmt.Fprintln(os.Stdout, version) | |
| println(version) |
println で良さそう。
| const terraformDir = "./" //TODO: あとで引数で弄れるようにしたい | ||
| func parseConfig(args []string, stdout io.Writer) (cfg config, showVersion bool, parseErr error) { | ||
| cfg = config{ | ||
| excludeDirs: append([]string(nil), defaultExcludedDirs...), |
There was a problem hiding this comment.
| excludeDirs: append([]string(nil), defaultExcludedDirs...), | |
| excludeDirs: append([]string{}, defaultExcludedDirs...), |
個人的にはこの方が好みかも。
| cfg.stdout = io.Discard | ||
| } | ||
| if len(cfg.excludeDirs) == 0 { | ||
| cfg.excludeDirs = append([]string(nil), defaultExcludedDirs...) |
There was a problem hiding this comment.
| cfg.excludeDirs = append([]string(nil), defaultExcludedDirs...) | |
| cfg.excludeDirs = append([]string{}, defaultExcludedDirs...) |
個人的にはこの方が好みかも。
| if cfg.stdout == nil { | ||
| cfg.stdout = io.Discard | ||
| } | ||
| if len(cfg.excludeDirs) == 0 { | ||
| cfg.excludeDirs = append([]string(nil), defaultExcludedDirs...) | ||
| } | ||
| if len(cfg.targets) == 0 { | ||
| cfg.targets = []string{"."} | ||
| } |
There was a problem hiding this comment.
この辺は parseConfig と処理が被っている感じがあるので、この辺は削除するか、初期化のメソッドを用意して、それをここと parseConfig から呼び出す形にすると良さそう。
| i++ // 後ろに改行がある場合はそれを削除 | ||
| i++ | ||
| } else if endTokenPos-2 > startTokenPos && | ||
| tokens[endTokenPos-1].Type == hclsyntax.TokenNewline && | ||
| tokens[endTokenPos-2].Type == hclsyntax.TokenNewline { | ||
| endTokenPos-- // 後ろに改行はないけど、上に二つ以上改行がある場合、一つ削除 |
| wf, err := root.OpenFile(relPath, os.O_WRONLY|os.O_TRUNC, perm) | ||
| if err != nil { | ||
| return errors.Wrap(err, "open file for write") | ||
| } | ||
| defer func() { | ||
| if closeErr := wf.Close(); closeErr != nil { | ||
| err = errors.Join(err, errors.Wrap(closeErr, "close file")) | ||
| } | ||
| }() | ||
|
|
||
| if _, err := wf.Write(src); err != nil { | ||
| return errors.Wrap(err, "write file") | ||
| } | ||
|
|
||
| return nil |
| src, err := readTFFile(root, relPath) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "read terraform file") | ||
| } | ||
|
|
||
| file, diags := hclwrite.ParseConfig(src, displayPath, hcl.InitialPos) | ||
| if diags.HasErrors() { | ||
| return false, errors.Wrap(diags, "parse terraform file") | ||
| } | ||
|
|
||
| ret := tfrbac(file.Body()).Bytes() |
| relPath, relErr := filepath.Rel(target, filePath) | ||
| if relErr != nil { | ||
| return errors.Wrap(relErr, "build relative path") | ||
| } |
| if !strings.HasSuffix(entry.Name(), ".tf") { | ||
| return nil | ||
| } |
| if walkErr != nil { | ||
| return errors.Wrap(walkErr, "Error on filepath.Walk") | ||
| return errors.Wrap(walkErr, "walk directory") | ||
| } |
| root, err := os.OpenRoot(target) | ||
| if err != nil { | ||
| return errors.Wrap(err, "Error on os.OpenRoot") | ||
| return 0, errors.Wrap(err, "open root") | ||
| } | ||
| defer func(root *os.Root) { | ||
| defer func() { | ||
| if closeErr := root.Close(); closeErr != nil { | ||
| err = errors.Join(err, errors.Wrap(closeErr, "Error on Close")) | ||
| resultErr = errors.Join(resultErr, errors.Wrap(closeErr, "close root")) | ||
| } | ||
| }(root) | ||
| }() |
| func parseConfig(args []string, stdout io.Writer) (cfg config, showVersion bool, parseErr error) { | ||
| cfg = config{ |
There was a problem hiding this comment.
| func parseConfig(args []string, stdout io.Writer) (cfg config, showVersion bool, parseErr error) { | |
| cfg = config{ | |
| func parseConfig(args []string, stdout io.Writer) (config, bool, error) { | |
| cfg := config{ |
戻り値の名前は定義しなくて良い気がする。
| root, err := os.OpenRoot(filepath.Dir(target)) | ||
| if err != nil { | ||
| return 0, errors.Wrap(err, "open file root") | ||
| } | ||
| defer func() { | ||
| if closeErr := root.Close(); closeErr != nil { | ||
| err = errors.Join(err, errors.Wrap(closeErr, "close file root")) | ||
| } | ||
| }() |
| }() | ||
|
|
||
| err = filepath.Walk(terraformDir, func(filePath string, info os.FileInfo, walkErr error) (err error) { | ||
| err = filepath.WalkDir(target, func(filePath string, entry fs.DirEntry, walkErr error) error { |
There was a problem hiding this comment.
filepath と filePath は紛らわしいので、 filePath を変えても良いかも。
参考: dev-hato/hato-bot-go#363
| return errors.Wrap(err, "Error on readTFFile") | ||
| pathKey, pathErr := uniquePathKey(filePath) | ||
| if pathErr != nil { | ||
| return pathErr |
There was a problem hiding this comment.
| return pathErr | |
| return errors.Wrap(pathErr, "uniquePathKey") |
pathErr をWrapしても良さそう。
| return errors.Wrap(err, "Error on Write") | ||
| changed, processErr := processTFFile(root, relPath, filePath, info.Mode().Perm(), cfg) | ||
| if processErr != nil { | ||
| return processErr |
There was a problem hiding this comment.
| return processErr | |
| return errors.Wrap(processErr, "processTFFile") |
processErr をWrapしても良さそう。
| pathKey, err := uniquePathKey(target) | ||
| if err != nil { | ||
| return errors.Wrap(err, "Error walking through Terraform directory") | ||
| return 0, err |
There was a problem hiding this comment.
| return 0, err | |
| return 0, errors.Wrap(err, "uniquePathKey") |
uniquePathKey をWrapしても良さそう。
|
|
||
| changed, err := processTFFile(root, filepath.Base(target), target, perm, cfg) | ||
| if err != nil { | ||
| return 0, err |
There was a problem hiding this comment.
| return 0, err | |
| return 0, errors.Wrap(err, "processTFFile") |
processTFFile をWrapしても良さそう。
codexに「いい感じにして」って投げたらこうなったんだが、あってるんだろうか・・・