Develop #3
2
Makefile
2
Makefile
@@ -5,7 +5,7 @@ CTN:=$(shell which podman 2>&1 1>/dev/null && echo "podman" || echo "docker")
|
|||||||
PREFIX=~/.local
|
PREFIX=~/.local
|
||||||
|
|
||||||
GO=container
|
GO=container
|
||||||
BLD_CMD=go build -o katenary -ldflags="-X 'main.Version=$(VERSION)'" .
|
BLD_CMD=go build -o katenary -ldflags="-X 'main.Version=$(VERSION)'" ./cmd/*.go
|
||||||
GOOS=linux
|
GOOS=linux
|
||||||
GOARCH=amd64
|
GOARCH=amd64
|
||||||
|
|
||||||
|
258
cmd/main.go
258
cmd/main.go
@@ -1,141 +1,151 @@
|
|||||||
package cmd
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"katenary/compose"
|
"katenary/generator/writers"
|
||||||
"katenary/generator"
|
"katenary/helm"
|
||||||
"os"
|
"katenary/update"
|
||||||
"os/exec"
|
"strconv"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var Version = "master" // changed at compile time
|
||||||
composeFiles = []string{"docker-compose.yaml", "docker-compose.yml"}
|
|
||||||
ComposeFile = ""
|
var longHelp = `Katenary aims to be a tool to convert docker-compose files to Helm Charts.
|
||||||
AppName = "MyApp"
|
It will create deployments, services, volumes, secrets, and ingress resources.
|
||||||
ChartsDir = "chart"
|
But it will also create initContainers based on depend_on, healthcheck, and other features.
|
||||||
Version = "master"
|
It's not magical, sometimes you'll need to fix the generated charts.
|
||||||
AppVersion = "0.0.1"
|
The general way to use it is to call one of these commands:
|
||||||
)
|
|
||||||
|
katenary convert
|
||||||
|
katenary convert -f docker-compose.yml
|
||||||
|
katenary convert -f docker-compose.yml -o ./charts
|
||||||
|
|
||||||
|
In case of, check the help of each command using:
|
||||||
|
katenary <command> --help
|
||||||
|
or
|
||||||
|
"katenary help <command>"
|
||||||
|
`
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
FindComposeFile()
|
// apply the version to the "update" package
|
||||||
SetAppName()
|
update.Version = Version
|
||||||
SetAppVersion()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FindComposeFile() bool {
|
func main() {
|
||||||
for _, file := range composeFiles {
|
|
||||||
if _, err := os.Stat(file); err == nil {
|
|
||||||
ComposeFile = file
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetAppName sets the application name from the current directory name.
|
// The base command
|
||||||
func SetAppName() {
|
rootCmd := &cobra.Command{
|
||||||
wd, err := os.Getwd()
|
Use: "katenary",
|
||||||
if err != nil {
|
Long: longHelp,
|
||||||
return
|
Short: "Katenary is a tool to convert docker-compose files to Helm Charts",
|
||||||
}
|
|
||||||
AppName = filepath.Base(wd)
|
|
||||||
|
|
||||||
if AppName == "" {
|
|
||||||
AppName = "MyApp"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetAppVersion set the AppVersion variable to the git version/tag
|
|
||||||
func SetAppVersion() {
|
|
||||||
AppVersion, _ = detectGitVersion()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try to detect the git version/tag.
|
|
||||||
func detectGitVersion() (string, error) {
|
|
||||||
defaulVersion := "0.0.1"
|
|
||||||
// Check if .git directory exists
|
|
||||||
if s, err := os.Stat(".git"); err != nil {
|
|
||||||
// .git should be a directory
|
|
||||||
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
|
// to display the version
|
||||||
if _, err := exec.LookPath("git"); err != nil {
|
versionCmd := &cobra.Command{
|
||||||
return defaulVersion, errors.New("git executable not found")
|
Use: "version",
|
||||||
|
Short: "Display version",
|
||||||
|
Run: func(c *cobra.Command, args []string) { c.Println(Version) },
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the latest commit hash
|
// convert command, need some flags
|
||||||
if out, err := exec.Command("git", "log", "-n1", "--pretty=format:%h").Output(); err == nil {
|
convertCmd := &cobra.Command{
|
||||||
latestCommit := strings.TrimSpace(string(out))
|
Use: "convert",
|
||||||
// then get the current branch/tag
|
Short: "Convert docker-compose to helm chart",
|
||||||
out, err := exec.Command("git", "branch", "--show-current").Output()
|
Long: "Convert docker-compose to helm chart. The resulting helm chart will be in the current directory/" +
|
||||||
if err != nil {
|
ChartsDir + "/" + AppName +
|
||||||
return defaulVersion, errors.New("git branch --show-current failed")
|
".\nThe appversion will be generated that way:\n" +
|
||||||
} else {
|
"- if it's in a git project, it takes git version or tag\n" +
|
||||||
currentBranch := strings.TrimSpace(string(out))
|
"- if it's not defined, so the version will be get from the --app-version flag \n" +
|
||||||
// finally, check if the current tag (if exists) correspond to the current commit
|
"- if it's not defined, so the 0.0.1 version is used",
|
||||||
// git describe --exact-match --tags <latestCommit>
|
Run: func(c *cobra.Command, args []string) {
|
||||||
out, err := exec.Command("git", "describe", "--exact-match", "--tags", latestCommit).Output()
|
force := c.Flag("force").Changed
|
||||||
if err == nil {
|
// TODO: is there a way to get typed values from cobra?
|
||||||
return strings.TrimSpace(string(out)), nil
|
appversion := c.Flag("app-version").Value.String()
|
||||||
} else {
|
composeFile := c.Flag("compose-file").Value.String()
|
||||||
return currentBranch + "-" + latestCommit, nil
|
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
|
||||||
}
|
}
|
||||||
}
|
Convert(composeFile, appversion, appName, chartDir, force)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return defaulVersion, errors.New("git log failed")
|
convertCmd.Flags().BoolP(
|
||||||
}
|
"force", "f", false, "force overwrite of existing output files")
|
||||||
|
convertCmd.Flags().StringP(
|
||||||
func Convert(composeFile, appVersion, appName, chartDir string, force bool) {
|
"app-version", "a", AppVersion, "app version")
|
||||||
composeFound := FindComposeFile()
|
convertCmd.Flags().StringP(
|
||||||
_, err := os.Stat(ComposeFile)
|
"compose-file", "c", ComposeFile, "docker compose file")
|
||||||
if !composeFound && err != nil {
|
convertCmd.Flags().StringP(
|
||||||
fmt.Println("No compose file found")
|
"app-name", "n", AppName, "application name")
|
||||||
os.Exit(1)
|
convertCmd.Flags().StringP(
|
||||||
}
|
"output-dir", "o", ChartsDir, "chart directory")
|
||||||
|
convertCmd.Flags().IntP(
|
||||||
dirname := filepath.Join(chartDir, appName)
|
"indent-size", "i", 2, "set the indent size of the YAML files")
|
||||||
if _, err := os.Stat(dirname); err == nil && !force {
|
|
||||||
response := ""
|
// show possible labels to set in docker-compose file
|
||||||
for response != "y" && response != "n" {
|
showLabelsCmd := &cobra.Command{
|
||||||
response = "n"
|
Use: "show-labels",
|
||||||
fmt.Printf(""+
|
Short: "Show labels of a resource",
|
||||||
"The %s directory already exists, it will be \x1b[31;1mremoved\x1b[0m!\n"+
|
Run: func(c *cobra.Command, args []string) {
|
||||||
"Do you really want to continue? [y/N]: ", dirname)
|
c.Println(helm.GetLabelsDocumentation())
|
||||||
fmt.Scanf("%s", &response)
|
},
|
||||||
response = strings.ToLower(response)
|
}
|
||||||
}
|
|
||||||
if response == "n" {
|
// Update the binary to the latest version
|
||||||
fmt.Println("Cancelled")
|
updateCmd := &cobra.Command{
|
||||||
os.Exit(0)
|
Use: "upgrade",
|
||||||
}
|
Short: "Upgrade katenary to the latest version if available",
|
||||||
}
|
Run: func(c *cobra.Command, args []string) {
|
||||||
|
version, assets, err := update.CheckLatestVersion()
|
||||||
// cleanup and create the chart directory (until "templates")
|
if err != nil {
|
||||||
if err := os.RemoveAll(dirname); err != nil {
|
c.Println(err)
|
||||||
fmt.Printf("Error removing %s: %s\n", dirname, err)
|
return
|
||||||
os.Exit(1)
|
}
|
||||||
}
|
c.Println("Updating to version: " + version)
|
||||||
|
err = update.DownloadLatestVersion(assets)
|
||||||
// create the templates directory
|
if err != nil {
|
||||||
templatesDir := filepath.Join(dirname, "templates")
|
c.Println(err)
|
||||||
if err := os.MkdirAll(templatesDir, 0755); err != nil {
|
return
|
||||||
fmt.Printf("Error creating %s: %s\n", templatesDir, err)
|
}
|
||||||
os.Exit(1)
|
c.Println("Update completed")
|
||||||
}
|
},
|
||||||
|
}
|
||||||
// Parse the compose file now
|
|
||||||
p := compose.NewParser(ComposeFile)
|
rootCmd.AddCommand(
|
||||||
p.Parse(appName)
|
versionCmd,
|
||||||
|
convertCmd,
|
||||||
// start generator
|
showLabelsCmd,
|
||||||
generator.Generate(p, Version, appName, appVersion, ComposeFile, dirname)
|
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
|
||||||
|
}
|
||||||
|
if Version != version {
|
||||||
|
ch <- fmt.Sprintf("\x1b[33mNew version available: " +
|
||||||
|
version +
|
||||||
|
" - to auto upgrade katenary, you can execute: katenary upgrade\x1b[0m\n")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Execute the command
|
||||||
|
finalize := make(chan error)
|
||||||
|
go func() {
|
||||||
|
finalize <- rootCmd.Execute()
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Wait for both goroutines to finish
|
||||||
|
if err := <-finalize; err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
fmt.Print(<-ch)
|
||||||
}
|
}
|
||||||
|
140
cmd/utils.go
Normal file
140
cmd/utils.go
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"katenary/compose"
|
||||||
|
"katenary/generator"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
composeFiles = []string{"docker-compose.yaml", "docker-compose.yml"}
|
||||||
|
ComposeFile = ""
|
||||||
|
AppName = "MyApp"
|
||||||
|
ChartsDir = "chart"
|
||||||
|
AppVersion = "0.0.1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
FindComposeFile()
|
||||||
|
SetAppName()
|
||||||
|
SetAppVersion()
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindComposeFile() bool {
|
||||||
|
for _, file := range composeFiles {
|
||||||
|
if _, err := os.Stat(file); err == nil {
|
||||||
|
ComposeFile = file
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAppName sets the application name from the current directory name.
|
||||||
|
func SetAppName() {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
AppName = filepath.Base(wd)
|
||||||
|
|
||||||
|
if AppName == "" {
|
||||||
|
AppName = "MyApp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAppVersion set the AppVersion variable to the git version/tag
|
||||||
|
func SetAppVersion() {
|
||||||
|
AppVersion, _ = detectGitVersion()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to detect the git version/tag.
|
||||||
|
func detectGitVersion() (string, error) {
|
||||||
|
defaulVersion := "0.0.1"
|
||||||
|
// Check if .git directory exists
|
||||||
|
if s, err := os.Stat(".git"); err != nil {
|
||||||
|
// .git should be a directory
|
||||||
|
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 {
|
||||||
|
return defaulVersion, errors.New("git executable not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the latest commit hash
|
||||||
|
if out, err := exec.Command("git", "log", "-n1", "--pretty=format:%h").Output(); err == nil {
|
||||||
|
latestCommit := strings.TrimSpace(string(out))
|
||||||
|
// then get the current branch/tag
|
||||||
|
out, err := exec.Command("git", "branch", "--show-current").Output()
|
||||||
|
if err != nil {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return defaulVersion, errors.New("git log failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Convert(composeFile, appVersion, appName, chartDir string, force bool) {
|
||||||
|
composeFound := FindComposeFile()
|
||||||
|
_, err := os.Stat(ComposeFile)
|
||||||
|
if !composeFound && err != nil {
|
||||||
|
fmt.Println("No compose file found")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
dirname := filepath.Join(chartDir, 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!\n"+
|
||||||
|
"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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the compose file now
|
||||||
|
p := compose.NewParser(ComposeFile)
|
||||||
|
p.Parse(appName)
|
||||||
|
|
||||||
|
// start generator
|
||||||
|
generator.Generate(p, Version, appName, appVersion, ComposeFile, dirname)
|
||||||
|
|
||||||
|
}
|
150
main.go
150
main.go
@@ -1,150 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"katenary/cmd"
|
|
||||||
"katenary/generator/writers"
|
|
||||||
"katenary/helm"
|
|
||||||
"katenary/update"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
)
|
|
||||||
|
|
||||||
var Version = "master" // changed at compile time
|
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
katenary convert
|
|
||||||
katenary convert -f docker-compose.yml
|
|
||||||
katenary convert -f docker-compose.yml -o ./charts
|
|
||||||
|
|
||||||
In case of, check the help of each command using:
|
|
||||||
katenary <command> --help
|
|
||||||
or
|
|
||||||
"katenary help <command>"
|
|
||||||
`
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
// set the version to "cmd" package - yes... it's ugly
|
|
||||||
cmd.Version = Version
|
|
||||||
|
|
||||||
// The base command
|
|
||||||
rootCmd := &cobra.Command{
|
|
||||||
Use: "katenary",
|
|
||||||
Long: longHelp,
|
|
||||||
Short: "Katenary is a tool to convert docker-compose files to Helm Charts",
|
|
||||||
}
|
|
||||||
|
|
||||||
// to display the version
|
|
||||||
versionCmd := &cobra.Command{
|
|
||||||
Use: "version",
|
|
||||||
Short: "Display version",
|
|
||||||
Run: func(c *cobra.Command, args []string) { c.Println(Version) },
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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/" +
|
|
||||||
cmd.ChartsDir + "/" + cmd.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
|
|
||||||
}
|
|
||||||
cmd.Convert(composeFile, appversion, appName, chartDir, force)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
convertCmd.Flags().BoolP(
|
|
||||||
"force", "f", false, "force overwrite of existing output files")
|
|
||||||
convertCmd.Flags().StringP(
|
|
||||||
"app-version", "a", cmd.AppVersion, "app version")
|
|
||||||
convertCmd.Flags().StringP(
|
|
||||||
"compose-file", "c", cmd.ComposeFile, "docker compose file")
|
|
||||||
convertCmd.Flags().StringP(
|
|
||||||
"app-name", "n", cmd.AppName, "application name")
|
|
||||||
convertCmd.Flags().StringP(
|
|
||||||
"output-dir", "o", cmd.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())
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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")
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
if cmd.Version != version {
|
|
||||||
ch <- fmt.Sprintf("\x1b[33mNew version available: " +
|
|
||||||
version +
|
|
||||||
" - to auto upgrade katenary, you can execute: katenary upgrade\x1b[0m\n")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Execute the command
|
|
||||||
finalize := make(chan error)
|
|
||||||
go func() {
|
|
||||||
finalize <- rootCmd.Execute()
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Wait for both goroutines to finish
|
|
||||||
if err := <-finalize; err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
fmt.Print(<-ch)
|
|
||||||
}
|
|
@@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"katenary/cmd"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -13,6 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var exe, _ = os.Executable()
|
var exe, _ = os.Executable()
|
||||||
|
var Version = "master" // reset by cmd/main.go
|
||||||
|
|
||||||
// Asset is a github asset from release url.
|
// Asset is a github asset from release url.
|
||||||
type Asset struct {
|
type Asset struct {
|
||||||
@@ -63,7 +63,7 @@ func CheckLatestVersion() (string, []Asset, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if the current version is the same as the latest version, don't update
|
// if the current version is the same as the latest version, don't update
|
||||||
if cmd.Version == release.TagName {
|
if Version == release.TagName {
|
||||||
fmt.Println("You are using the latest version")
|
fmt.Println("You are using the latest version")
|
||||||
return "", nil, errors.New("You are using the latest version")
|
return "", nil, errors.New("You are using the latest version")
|
||||||
}
|
}
|
||||||
|
@@ -2,15 +2,14 @@ package update
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"katenary/cmd"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDownloadLatestRelease(t *testing.T) {
|
func TestDownloadLatestRelease(t *testing.T) {
|
||||||
|
|
||||||
// Change the cmd.Version to "v0.0.0" to test the fallback to the latest release
|
// Reset the version to test the latest release
|
||||||
cmd.Version = "v0.0.0"
|
Version = "0.0.0"
|
||||||
|
|
||||||
// change "exe" to /tmp/test-katenary
|
// change "exe" to /tmp/test-katenary
|
||||||
exe = "/tmp/test-katenary"
|
exe = "/tmp/test-katenary"
|
||||||
|
Reference in New Issue
Block a user