2024-04-10 04:49:36 +02:00
// Katenary CLI, main package.
//
// This package is not intended to be imported. It contains the
// main function that build the command line with `cobra` package.
2022-03-31 14:12:20 +02:00
package main
2022-02-14 17:00:32 +01:00
import (
"fmt"
2023-12-06 15:24:02 +01:00
"os"
"strings"
2022-02-14 17:00:32 +01:00
2024-04-05 07:56:27 +02:00
"katenary/generator"
"katenary/utils"
2023-12-06 15:24:02 +01:00
"github.com/compose-spec/compose-go/cli"
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
2023-12-06 15:24:02 +01:00
const longHelp = ` Katenary is a tool to convert compose files to Helm Charts .
2022-02-14 17:00:32 +01:00
2023-12-06 15:24:02 +01:00
Each [ command ] and subcommand has got an "help" and "--help" flag to show more information .
2022-03-31 14:12:20 +02:00
`
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
}
2023-12-06 15:24:02 +01:00
rootCmd . Example = ` katenary convert -c docker-compose.yml -o ./charts `
2022-02-14 17:00:32 +01:00
2023-12-06 15:24:02 +01:00
rootCmd . Version = generator . Version
rootCmd . CompletionOptions . DisableDescriptions = false
rootCmd . CompletionOptions . DisableNoDescFlag = false
rootCmd . AddCommand (
generateCompletionCommand ( rootCmd . Name ( ) ) ,
generateVersionCommand ( ) ,
generateConvertCommand ( ) ,
generateHashComposefilesCommand ( ) ,
generateLabelHelpCommand ( ) ,
)
rootCmd . Execute ( )
}
const completionHelp = ` To load completions :
Bash :
# Add this line in your ~ / . bashrc or ~ / . bash_profile file
$ source < ( % [ 1 ] s completion bash )
# Or , you can load completions for each users session . Execute once :
# Linux :
$ % [ 1 ] s completion bash > / etc / bash_completion . d / % [ 1 ] s
# macOS :
$ % [ 1 ] s completion bash > $ ( brew -- prefix ) / etc / bash_completion . d / % [ 1 ] s
Zsh :
# If shell completion is not already enabled in your environment ,
# you will need to enable it . You can execute the following once :
$ echo "autoload -U compinit; compinit" >> ~ / . zshrc
# To load completions for each session , execute once :
$ % [ 1 ] s completion zsh > "${fpath[1]}/_%[1]s"
# You will need to start a new shell for this setup to take effect .
fish :
$ % [ 1 ] s completion fish | source
# To load completions for each session , execute once :
$ % [ 1 ] s completion fish > ~ / . config / fish / completions / % [ 1 ] s . fish
PowerShell :
PS > % [ 1 ] s completion powershell | Out - String | Invoke - Expression
# To load completions for every new session , run :
PS > % [ 1 ] s completion powershell > % [ 1 ] s . ps1
# and source this file from your PowerShell profile .
`
func generateCompletionCommand ( name string ) * cobra . Command {
bashV1 := false
cmd := & cobra . Command {
Use : "completion" ,
DisableFlagsInUseLine : true ,
ValidArgs : [ ] string { "bash" , "zsh" , "fish" , "powershell" } ,
Args : cobra . MatchAll ( cobra . ExactArgs ( 1 ) , cobra . OnlyValidArgs ) ,
Short : "Generates completion scripts" ,
Long : fmt . Sprintf ( completionHelp , name ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
if len ( args ) == 0 {
cmd . Help ( )
return
}
switch args [ 0 ] {
case "bash" :
// get the bash version
if cmd . Flags ( ) . Changed ( "bash-v1" ) {
cmd . Root ( ) . GenBashCompletion ( os . Stdout )
return
}
cmd . Root ( ) . GenBashCompletionV2 ( os . Stdout , true )
case "zsh" :
cmd . Root ( ) . GenZshCompletion ( os . Stdout )
case "fish" :
cmd . Root ( ) . GenFishCompletion ( os . Stdout , true )
case "powershell" :
cmd . Root ( ) . GenPowerShellCompletion ( os . Stdout )
}
} ,
2022-02-14 17:00:32 +01:00
}
2023-12-06 15:24:02 +01:00
// add a flag to force bash completion v1
cmd . Flags ( ) . Bool ( "bash-v1" , bashV1 , "Force bash completion v1" )
return cmd
}
func generateConvertCommand ( ) * cobra . Command {
force := false
outputDir := "./chart"
dockerComposeFile := make ( [ ] string , 0 )
profiles := make ( [ ] string , 0 )
helmdepUpdate := false
var appVersion * string
givenAppVersion := ""
chartVersion := "0.1.0"
2022-03-31 14:12:20 +02:00
convertCmd := & cobra . Command {
Use : "convert" ,
2023-12-06 15:24:02 +01:00
Short : "Converts a docker-compose file to a Helm Chart" ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
if givenAppVersion != "" {
appVersion = & givenAppVersion
2022-02-14 17:00:32 +01:00
}
2023-12-06 15:24:02 +01:00
generator . Convert ( generator . ConvertOptions {
Force : force ,
OutputDir : outputDir ,
Profiles : profiles ,
HelmUpdate : helmdepUpdate ,
AppVersion : appVersion ,
ChartVersion : chartVersion ,
} , dockerComposeFile ... )
2022-03-31 14:12:20 +02:00
} ,
}
2023-12-06 15:24:02 +01:00
convertCmd . Flags ( ) . BoolVarP ( & force , "force" , "f" , force , "Force the overwrite of the chart directory" )
convertCmd . Flags ( ) . BoolVarP ( & helmdepUpdate , "helm-update" , "u" , helmdepUpdate , "Update helm dependencies if helm is installed" )
convertCmd . Flags ( ) . StringSliceVarP ( & profiles , "profile" , "p" , profiles , "Specify the profiles to use" )
convertCmd . Flags ( ) . StringVarP ( & outputDir , "output-dir" , "o" , outputDir , "Specify the output directory" )
2024-04-19 22:26:45 +02:00
convertCmd . Flags ( ) . StringSliceVarP ( & dockerComposeFile , "compose-file" , "c" , cli . DefaultFileNames , "Specify an alternate compose files - can be specified multiple times or use coma to separate them.\nNote that overides files are also used whatever the files you specify here.\nThe overides files are:\n" + strings . Join ( cli . DefaultOverrideFileNames , ", \n" ) + "\n" )
2023-12-06 15:24:02 +01:00
convertCmd . Flags ( ) . StringVarP ( & givenAppVersion , "app-version" , "a" , "" , "Specify the app version (in Chart.yaml)" )
convertCmd . Flags ( ) . StringVarP ( & chartVersion , "chart-version" , "v" , chartVersion , "Specify the chart version (in Chart.yaml)" )
return convertCmd
}
func generateVersionCommand ( ) * cobra . Command {
return & cobra . Command {
Use : "version" ,
Short : "Print the version number of Katenary" ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
println ( generator . Version )
2022-03-31 14:12:20 +02:00
} ,
2022-02-14 17:00:32 +01:00
}
2023-12-06 15:24:02 +01:00
}
2022-02-14 17:00:32 +01:00
2023-12-06 15:24:02 +01:00
func generateLabelHelpCommand ( ) * cobra . Command {
markdown := false
all := false
cmd := & cobra . Command {
Use : "help-labels [label]" ,
Short : "Print the labels help for all or a specific label" ,
Long : ` Print the labels help for all or a specific label
If no label is specified , the help for all labels is printed .
If a label is specified , the help for this label is printed .
The name of the label must be specified without the prefix ` + generator.KATENARY_PREFIX + ` .
e . g .
kanetary help - labels
katenary help - labels ingress
katenary help - labels map - env
` ,
ValidArgs : generator . GetLabelNames ( ) ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
if len ( args ) > 0 {
fmt . Println ( generator . GetLabelHelpFor ( args [ 0 ] , markdown ) )
2022-03-31 14:12:20 +02:00
return
}
2023-12-06 15:24:02 +01:00
if all {
// show the help for all labels
l := len ( generator . GetLabelNames ( ) )
for i , label := range generator . GetLabelNames ( ) {
fmt . Println ( generator . GetLabelHelpFor ( label , markdown ) )
if ! markdown && i < l - 1 {
fmt . Println ( strings . Repeat ( "-" , 80 ) )
}
}
2022-03-31 14:12:20 +02:00
return
}
2023-12-06 15:24:02 +01:00
fmt . Println ( generator . GetLabelHelp ( markdown ) )
2022-03-31 14:12:20 +02:00
} ,
2022-02-14 17:00:32 +01:00
}
2023-12-06 15:24:02 +01:00
cmd . Flags ( ) . BoolVarP ( & markdown , "markdown" , "m" , markdown , "Use the markdown format" )
cmd . Flags ( ) . BoolVarP ( & all , "all" , "a" , all , "Print the full help for all labels" )
2022-03-31 14:12:20 +02:00
2023-12-06 15:24:02 +01:00
return cmd
}
func generateHashComposefilesCommand ( ) * cobra . Command {
cmd := & cobra . Command {
Use : "hash-composefiles [composefile]" ,
Short : "Print the hash of the composefiles" ,
Long : ` Print the hash of the composefiles
If no composefile is specified , the hash of all composefiles is printed . ` ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
if len ( args ) > 0 {
if hash , err := utils . HashComposefiles ( args ) ; err != nil {
fmt . Println ( err )
} else {
fmt . Println ( hash )
}
return
}
} ,
2022-02-14 17:00:32 +01:00
}
2023-12-06 15:24:02 +01:00
return cmd
2022-02-14 17:00:32 +01:00
}