2022-03-31 14:12:20 +02:00
|
|
|
package main
|
2022-02-14 17:00:32 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-03-31 14:12:20 +02:00
|
|
|
"katenary/generator/writers"
|
|
|
|
"katenary/helm"
|
|
|
|
"katenary/update"
|
|
|
|
"strconv"
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
"github.com/spf13/cobra"
|
2022-02-14 17:15:11 +01:00
|
|
|
)
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
var Version = "master" // changed at compile time
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
var longHelp = `Katenary aims to be a tool to convert docker-compose files to Helm Charts.
|
|
|
|
It will create deployments, services, volumes, secrets, and ingress resources.
|
|
|
|
But it will also create initContainers based on depend_on, healthcheck, and other features.
|
|
|
|
It's not magical, sometimes you'll need to fix the generated charts.
|
|
|
|
The general way to use it is to call one of these commands:
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
katenary convert
|
|
|
|
katenary convert -c docker-compose.yml
|
|
|
|
katenary convert -c docker-compose.yml -o ./charts
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
In case of, check the help of each command using:
|
|
|
|
katenary <command> --help
|
|
|
|
or
|
|
|
|
"katenary help <command>"
|
|
|
|
`
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
func init() {
|
|
|
|
// apply the version to the "update" package
|
|
|
|
update.Version = Version
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
// The base command
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
Use: "katenary",
|
|
|
|
Long: longHelp,
|
|
|
|
Short: "Katenary is a tool to convert docker-compose files to Helm Charts",
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
// to display the version
|
|
|
|
versionCmd := &cobra.Command{
|
|
|
|
Use: "version",
|
|
|
|
Short: "Display version",
|
|
|
|
Run: func(c *cobra.Command, args []string) { c.Println(Version) },
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
// convert command, need some flags
|
|
|
|
convertCmd := &cobra.Command{
|
|
|
|
Use: "convert",
|
|
|
|
Short: "Convert docker-compose to helm chart",
|
|
|
|
Long: "Convert docker-compose to helm chart. The resulting helm chart will be in the current directory/" +
|
|
|
|
ChartsDir + "/" + AppName +
|
|
|
|
".\nThe appversion will be generated that way:\n" +
|
|
|
|
"- if it's in a git project, it takes git version or tag\n" +
|
|
|
|
"- if it's not defined, so the version will be get from the --app-version flag \n" +
|
|
|
|
"- if it's not defined, so the 0.0.1 version is used",
|
|
|
|
Run: func(c *cobra.Command, args []string) {
|
|
|
|
force := c.Flag("force").Changed
|
|
|
|
// TODO: is there a way to get typed values from cobra?
|
|
|
|
appversion := c.Flag("app-version").Value.String()
|
|
|
|
composeFile := c.Flag("compose-file").Value.String()
|
|
|
|
appName := c.Flag("app-name").Value.String()
|
|
|
|
chartDir := c.Flag("output-dir").Value.String()
|
|
|
|
indentation, err := strconv.Atoi(c.Flag("indent-size").Value.String())
|
|
|
|
if err != nil {
|
|
|
|
writers.IndentSize = indentation
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
2022-03-31 14:12:20 +02:00
|
|
|
Convert(composeFile, appversion, appName, chartDir, force)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
convertCmd.Flags().BoolP(
|
|
|
|
"force", "f", false, "force overwrite of existing output files")
|
|
|
|
convertCmd.Flags().StringP(
|
|
|
|
"app-version", "a", AppVersion, "app version")
|
|
|
|
convertCmd.Flags().StringP(
|
|
|
|
"compose-file", "c", ComposeFile, "docker compose file")
|
|
|
|
convertCmd.Flags().StringP(
|
|
|
|
"app-name", "n", AppName, "application name")
|
|
|
|
convertCmd.Flags().StringP(
|
|
|
|
"output-dir", "o", ChartsDir, "chart directory")
|
|
|
|
convertCmd.Flags().IntP(
|
|
|
|
"indent-size", "i", 2, "set the indent size of the YAML files")
|
|
|
|
|
|
|
|
// show possible labels to set in docker-compose file
|
|
|
|
showLabelsCmd := &cobra.Command{
|
|
|
|
Use: "show-labels",
|
|
|
|
Short: "Show labels of a resource",
|
|
|
|
Run: func(c *cobra.Command, args []string) {
|
|
|
|
c.Println(helm.GetLabelsDocumentation())
|
|
|
|
},
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
// Update the binary to the latest version
|
|
|
|
updateCmd := &cobra.Command{
|
|
|
|
Use: "upgrade",
|
|
|
|
Short: "Upgrade katenary to the latest version if available",
|
|
|
|
Run: func(c *cobra.Command, args []string) {
|
|
|
|
version, assets, err := update.CheckLatestVersion()
|
|
|
|
if err != nil {
|
|
|
|
c.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Println("Updating to version: " + version)
|
|
|
|
err = update.DownloadLatestVersion(assets)
|
|
|
|
if err != nil {
|
|
|
|
c.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Println("Update completed")
|
|
|
|
},
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
rootCmd.AddCommand(
|
|
|
|
versionCmd,
|
|
|
|
convertCmd,
|
|
|
|
showLabelsCmd,
|
|
|
|
updateCmd,
|
|
|
|
)
|
|
|
|
|
|
|
|
// in parallel, check if the current katenary version is the latest
|
|
|
|
ch := make(chan string)
|
|
|
|
go func() {
|
|
|
|
version, _, err := update.CheckLatestVersion()
|
|
|
|
if err != nil {
|
|
|
|
ch <- ""
|
|
|
|
return
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
2022-03-31 14:12:20 +02:00
|
|
|
if Version != version {
|
|
|
|
ch <- fmt.Sprintf("\x1b[33mNew version available: " +
|
|
|
|
version +
|
|
|
|
" - to auto upgrade katenary, you can execute: katenary upgrade\x1b[0m\n")
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
2022-03-31 14:12:20 +02:00
|
|
|
}()
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
// Execute the command
|
|
|
|
finalize := make(chan error)
|
|
|
|
go func() {
|
|
|
|
finalize <- rootCmd.Execute()
|
|
|
|
}()
|
2022-02-14 17:00:32 +01:00
|
|
|
|
2022-03-31 14:12:20 +02:00
|
|
|
// Wait for both goroutines to finish
|
|
|
|
if err := <-finalize; err != nil {
|
|
|
|
fmt.Println(err)
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|
2022-03-31 14:12:20 +02:00
|
|
|
fmt.Print(<-ch)
|
2022-02-14 17:00:32 +01:00
|
|
|
}
|