Files
katenary/main.go

151 lines
4.3 KiB
Go
Raw Normal View History

2021-11-30 12:04:28 +01:00
package main
import (
"errors"
2021-11-30 12:04:28 +01:00
"flag"
2021-12-01 08:31:51 +01:00
"fmt"
"katenary/compose"
"katenary/generator"
"katenary/helm"
2021-11-30 12:04:28 +01:00
"os"
"os/exec"
2021-11-30 12:04:28 +01:00
"path/filepath"
2021-11-30 12:09:07 +01:00
"strings"
2021-11-30 12:04:28 +01:00
)
var composeFiles = []string{"docker-compose.yaml", "docker-compose.yml"}
var ComposeFile = ""
2021-11-30 12:04:28 +01:00
var AppName = "MyApp"
var Version = "master" // set at build time to the git version/tag
2021-12-01 10:36:43 +01:00
var ChartsDir = "chart"
2021-11-30 12:04:28 +01:00
2022-02-02 10:30:32 +01:00
func findComposeFile() bool {
for _, file := range composeFiles {
if _, err := os.Stat(file); err == nil {
ComposeFile = file
2022-02-02 10:30:32 +01:00
return true
}
}
2022-02-02 10:30:32 +01:00
return false
}
func detectGitVersion() (string, error) {
2021-12-17 11:46:35 +01:00
defaulVersion := "0.0.1"
// Check if .git directory exists
2021-12-17 11:40:56 +01:00
if s, err := os.Stat(".git"); err != nil {
// .git should be a directory
2021-12-17 11:46:35 +01:00
return defaulVersion, errors.New("no git repository found")
} else if !s.IsDir() {
// .git should be a directory
return defaulVersion, errors.New(".git is not a directory")
}
// check if "git" executable is callable
if _, err := exec.LookPath("git"); err != nil {
2021-12-17 11:46:35 +01:00
return defaulVersion, errors.New("git executable not found")
}
2021-12-17 11:48:32 +01:00
// get the latest commit hash
if out, err := exec.Command("git", "log", "-n1", "--pretty=format:%h").Output(); err == nil {
latestCommit := strings.TrimSpace(string(out))
2021-12-17 11:48:32 +01:00
// then get the current branch/tag
out, err := exec.Command("git", "branch", "--show-current").Output()
if err != nil {
2021-12-17 11:46:35 +01:00
return defaulVersion, errors.New("git branch --show-current failed")
} else {
currentBranch := strings.TrimSpace(string(out))
// finally, check if the current tag (if exists) correspond to the current commit
// git describe --exact-match --tags <latestCommit>
out, err := exec.Command("git", "describe", "--exact-match", "--tags", latestCommit).Output()
if err == nil {
return strings.TrimSpace(string(out)), nil
} else {
return currentBranch + "-" + latestCommit, nil
}
}
}
2021-12-17 11:46:35 +01:00
return defaulVersion, errors.New("git log failed")
}
2021-11-30 12:04:28 +01:00
func main() {
appVersion := "0.0.1"
helpMessageForAppversion := "The version of the application. " +
"Default is 0.0.1. If you are using git, it will be the git version. " +
"Otherwise, it will be the branch name and the commit hash."
if v, err := detectGitVersion(); err == nil {
appVersion = v
helpMessageForAppversion = "The version of the application. " +
"If not set, the version will be detected from git."
}
// flags
2022-02-02 10:30:32 +01:00
composeFound := findComposeFile()
2021-12-01 10:36:43 +01:00
flag.StringVar(&ChartsDir, "chart-dir", ChartsDir, "set the chart directory")
2021-11-30 12:04:28 +01:00
flag.StringVar(&ComposeFile, "compose", ComposeFile, "set the compose file to parse")
flag.StringVar(&AppName, "appname", helm.GetProjectName(), "set the helm chart app name")
flag.StringVar(&appVersion, "appversion", appVersion, helpMessageForAppversion)
2021-12-17 10:35:21 +01:00
version := flag.Bool("version", false, "show version and exit")
2021-12-01 10:36:43 +01:00
force := flag.Bool("force", false, "force the removal of the chart-dir")
showLabels := flag.Bool("labels", false, "show possible labels and exit")
2021-11-30 12:04:28 +01:00
flag.Parse()
if *showLabels {
// display labels from helm/types.go
fmt.Println(helm.GetLabelsDocumentation())
os.Exit(0)
}
// Only display the version
2021-12-01 08:31:51 +01:00
if *version {
2021-12-01 09:27:12 +01:00
fmt.Println(Version)
2021-12-01 08:31:51 +01:00
os.Exit(0)
}
2022-02-02 10:30:32 +01:00
_, err := os.Stat(ComposeFile)
if !composeFound && err != nil {
fmt.Println("No compose file found")
os.Exit(1)
}
dirname := filepath.Join(ChartsDir, AppName)
2021-12-01 10:36:43 +01:00
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!\n"+
"Do you really want to continue? [y/N]: ", dirname)
2021-12-01 10:36:43 +01:00
fmt.Scanf("%s", &response)
response = strings.ToLower(response)
}
if response == "n" {
2021-12-01 10:47:20 +01:00
fmt.Println("Cancelled")
2021-12-01 10:36:43 +01:00
os.Exit(0)
}
}
// cleanup and create the chart directory (until "templates")
if err := os.RemoveAll(dirname); err != nil {
fmt.Printf("Error removing %s: %s\n", dirname, err)
os.Exit(1)
}
// create the templates directory
2021-12-01 10:36:43 +01:00
templatesDir := filepath.Join(dirname, "templates")
if err := os.MkdirAll(templatesDir, 0755); err != nil {
fmt.Printf("Error creating %s: %s\n", templatesDir, err)
os.Exit(1)
}
2021-12-01 10:36:43 +01:00
// Parse the compose file now
2021-11-30 12:04:28 +01:00
p := compose.NewParser(ComposeFile)
p.Parse(AppName)
// start generator
generator.Generate(p, Version, AppName, appVersion, ComposeFile, dirname)
2021-11-30 12:04:28 +01:00
}