From abb7daa5984642a8c1fe44cf9ab962b1e1b15b6e Mon Sep 17 00:00:00 2001 From: rick Date: Wed, 22 May 2024 16:29:39 +0800 Subject: [PATCH] feat: avoid overwrite the exist Dockerfile --- cmd/new-dockerfile/main.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cmd/new-dockerfile/main.go b/cmd/new-dockerfile/main.go index 16d3b17..794e9c5 100644 --- a/cmd/new-dockerfile/main.go +++ b/cmd/new-dockerfile/main.go @@ -28,6 +28,8 @@ func main() { flag.BoolVar(&quiet, "quiet", false, "Disable all log output except errors") var write bool flag.BoolVar(&write, "write", false, "Write the Dockerfile to disk at ./Dockerfile") + var overwrite bool + flag.BoolVar(&overwrite, "overwrite", false, "Overwrite the Dockerfile if it already exists") flag.Parse() level := slog.LevelInfo @@ -46,6 +48,14 @@ func main() { log := slog.New(handler) df := dockerfile.New(log) + // jump out if users don't want to overwrite the Dockerfile + if write && !overwrite { + if _, err := os.Stat(filepath.Join(path, "Dockerfile")); err == nil { + log.Error("Dockerfile already exists. Use --overwrite to overwrite it.") + return + } + } + viper.SetConfigName("new-dockerfile") viper.SetConfigType("yaml") viper.SetConfigType("yml")