From e496c24195078799816e3aface48fc08661ae190 Mon Sep 17 00:00:00 2001 From: Patrice Ferlet Date: Wed, 1 Dec 2021 10:36:43 +0100 Subject: [PATCH] Check chart directory existence now --- README.md | 14 ++++++++------ main.go | 34 ++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e61df3e..1320d02 100644 --- a/README.md +++ b/README.md @@ -26,18 +26,20 @@ sudo make install PREFIX=/usr/local ```bash Usage of katenary: -appname string - Give the helm chart app name (default "MyApp") + sive the helm chart app name (default "MyApp") -appversion string - Set the chart appVersion (default "0.1.1") + set the chart appVersion (default "0.0.1") + -chart-dir string + set the chart directory (default "chart") -compose string set the compose file to parse (default "docker-compose.yaml") + -force + force the removal of the chart-dir -version - Show version and exist + Show version and exit ``` -Katenary will try to find a `docker-compose.yaml` file inside the current directory. It will then **delete** the `chart` directory to create a new Helm Chart. - -> It's important that you keep in mind that katenary will not alert you (for now) that it will remove the `chart` directory ! +Katenary will try to find a `docker-compose.yaml` file inside the current directory. It will check *the existence of the `chart` directory to create a new Helm Chart inside a named subdirectory. Katenary will ask you if you want to delete it before recreating. It creates a subdirectory inside `chart` that is named with the `appname` option (default is `MyApp`) diff --git a/main.go b/main.go index 4f7cd94..88419e2 100644 --- a/main.go +++ b/main.go @@ -19,13 +19,16 @@ var ComposeFile = "docker-compose.yaml" var AppName = "MyApp" var AppVersion = "0.0.1" var Version = "master" +var ChartsDir = "chart" func main() { + flag.StringVar(&ChartsDir, "chart-dir", ChartsDir, "set the chart directory") flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse") - flag.StringVar(&AppName, "appname", AppName, "Give the helm chart app name") - flag.StringVar(&AppVersion, "appversion", AppVersion, "Set the chart appVersion") - version := flag.Bool("version", false, "Show version and exist") + flag.StringVar(&AppName, "appname", AppName, "sive the helm chart app name") + flag.StringVar(&AppVersion, "appversion", AppVersion, "set the chart appVersion") + version := flag.Bool("version", false, "Show version and exit") + force := flag.Bool("force", false, "force the removal of the chart-dir") flag.Parse() if *version { @@ -33,6 +36,26 @@ func main() { os.Exit(0) } + dirname := filepath.Join(ChartsDir, AppName) + + if _, err := os.Stat(dirname); err == nil && !*force { + response := "" + for response != "y" && response != "n" { + response = "n" + fmt.Printf("The %s directory already exists, it will be \x1b[31;1mremoved\x1b[0m, do you really want to continue ? [y/N]: ", dirname) + fmt.Scanf("%s", &response) + response = strings.ToLower(response) + } + if response == "n" { + fmt.Println("Cancelled...") + os.Exit(0) + } + } + + os.RemoveAll(dirname) + templatesDir := filepath.Join(dirname, "templates") + os.MkdirAll(templatesDir, 0755) + helm.Version = Version p := compose.NewParser(ComposeFile) p.Parse(AppName) @@ -53,11 +76,6 @@ func main() { } wait.Wait() - dirname := filepath.Join("chart", AppName) - os.RemoveAll(dirname) - templatesDir := filepath.Join(dirname, "templates") - os.MkdirAll(templatesDir, 0755) - // to generate notes, we need to keep an Ingresses list ingresses := make(map[string]*helm.Ingress)