add(cmd): replaced go integrated cli flags with cobra pkg flags

This commit is contained in:
Markus Pesch 2019-11-12 18:30:16 +01:00
parent cfcfbdeee8
commit 0411a0ef61
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
5 changed files with 130 additions and 118 deletions

View File

@ -2,8 +2,7 @@
# If no version is specified as a parameter of make, the last git hash # If no version is specified as a parameter of make, the last git hash
# value is taken. # value is taken.
# VERSION?=$(shell git describe --abbrev=0)+hash.$(shell git rev-parse --short HEAD) # VERSION?=$(shell git describe --abbrev=0)+hash.$(shell git rev-parse --short HEAD)
VERSION?=$(shell git rev-parse --short HEAD) VERSION?=$(shell git describe --abbrev=0)+hash.$(shell git rev-parse --short HEAD)
RELEASE?=1
# EXECUTABLE # EXECUTABLE
# Executable binary which should be compiled for different architecures # Executable binary which should be compiled for different architecures
@ -62,19 +61,22 @@ README_FILE:=README.md
# BINARIES # BINARIES
# ============================================================================== # ==============================================================================
PHONY:=all PHONY:=all
${EXECUTABLE}: bin/tmp/${EXECUTABLE}
all: ${EXECUTABLE_TARGETS} all: ${EXECUTABLE_TARGETS}
bin/linux/amd64/$(EXECUTABLE): bindata bin/linux/amd64/$(EXECUTABLE): bindata
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "$@" CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "${@}"
bin/linux/arm/5/$(EXECUTABLE): bindata bin/linux/arm/5/$(EXECUTABLE): bindata
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "$@" CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "${@}"
bin/linux/arm/7/$(EXECUTABLE): bindata bin/linux/arm/7/$(EXECUTABLE): bindata
CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "$@" CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags "-X main.version=${VERSION}" -o "${@}"
bin/tmp/${EXECUTABLE}: bindata bin/tmp/${EXECUTABLE}: bindata
go build -ldflags "-X main.version=${VERSION}" -o "$@" go build -ldflags "-X main.version=${VERSION}" -o "${@}"
# BINDATA # BINDATA
# ============================================================================== # ==============================================================================

116
cmd/root.go Normal file
View File

@ -0,0 +1,116 @@
package cmd
import (
"io/ioutil"
"log"
"os"
"github.com/go-flucky/flucky/pkg/logger"
"github.com/spf13/cobra"
"github.com/volker-raschek/docker-hub-description-updater/pkg/hub"
"github.com/volker-raschek/docker-hub-description-updater/pkg/types"
)
var (
dockerHubNamespace string
dockerHubUser string
dockerHubPassword string
dockerHubRepository string
file string
loglevel string
flogger logger.Logger = logger.NewDefaultLogger(logger.LogLevelDebug)
)
var rootCmd = &cobra.Command{
Use: "dhdu",
Short: "docker hub description updater (dhdu)",
Run: func(cmd *cobra.Command, args []string) {
switch loglevel {
case "debug":
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
break
case "info":
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
break
case "warn":
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
break
case "error":
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
break
case "fatal":
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
break
}
if len(dockerHubUser) <= 0 {
flogger.Fatal("No user defined over flags")
}
if len(dockerHubPassword) <= 0 {
flogger.Fatal("No password defined over flags")
}
if len(dockerHubNamespace) <= 0 {
flogger.Debug("No namespace defined over flags: Use docker username instead")
dockerHubNamespace = dockerHubUser
}
if len(dockerHubRepository) <= 0 {
flogger.Fatal("No repository defined over flags")
}
if _, err := os.Stat(file); os.IsNotExist(err) && len(file) <= 0 {
flogger.Fatal("Can not find file: %v", file)
}
flogger.Debug("Read file %v", file)
f, err := ioutil.ReadFile(file)
if err != nil {
flogger.Fatal("Can not read file %v: %v", file, err)
}
fullDescription := string(f)
loginCredentials := &types.LoginCredentials{
User: dockerHubUser,
Password: dockerHubPassword,
}
flogger.Debug("Get Token")
token, err := hub.GetToken(loginCredentials)
if err != nil {
log.Fatalf("%v", err)
}
repository := &types.Repository{
Name: dockerHubRepository,
Namespcace: dockerHubNamespace,
FullDescription: fullDescription,
}
flogger.Debug("Send Repository Patch")
_, err = hub.PatchRepository(repository, token)
if err != nil {
log.Fatalf("%v", err)
}
},
}
// Execute a
func Execute(version string) {
rootCmd.Version = version
rootCmd.Flags().StringVarP(&loglevel, "loglevel", "l", "info", "Loglevel, possible values: debug, info, warn, error, fatal")
rootCmd.Flags().StringVarP(&dockerHubNamespace, "namespace", "n", "", "Docker Hub Namespace (default \"username\")")
rootCmd.Flags().StringVarP(&dockerHubPassword, "password", "p", "", "Docker Hub Password")
rootCmd.Flags().StringVarP(&dockerHubRepository, "repository", "r", "", "Docker Hub Repository")
rootCmd.Flags().StringVarP(&dockerHubUser, "username", "u", "", "Docker Hub Username")
rootCmd.Flags().StringVarP(&file, "file", "f", "./README.md", "File which should be uploaded as docker hub description")
rootCmd.Execute()
}

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/Masterminds/semver v1.5.0 github.com/Masterminds/semver v1.5.0
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-flucky/flucky v0.0.0-20190714170626-0dd156f480be github.com/go-flucky/flucky v0.0.0-20190714170626-0dd156f480be
github.com/spf13/cobra v0.0.3
github.com/stretchr/testify v1.4.0 github.com/stretchr/testify v1.4.0
github.com/volker-raschek/go-logger v0.0.0-20190924133618-4a42099e655f github.com/volker-raschek/go-logger v0.0.0-20190924133618-4a42099e655f
) )

2
go.sum
View File

@ -18,7 +18,9 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8= github.com/stianeikeland/go-rpio v4.2.0+incompatible/go.mod h1:Sh81rdJwD96E2wja2Gd7rrKM+XZ9LrwvN2w4IXrqLR8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

113
main.go
View File

@ -1,122 +1,13 @@
package main package main
import ( import (
"flag" "github.com/volker-raschek/docker-hub-description-updater/cmd"
"io/ioutil"
"log"
"github.com/Masterminds/semver"
"github.com/go-flucky/flucky/pkg/logger"
"github.com/volker-raschek/docker-hub-description-updater/pkg/hub"
"github.com/volker-raschek/docker-hub-description-updater/pkg/types"
) )
var ( var (
dockerHubAPI string = "https://hub.docker.com/v2"
dockerHubUser string
dockerHubPassword string
dockerHubNamespace string
dockerHubRepository string
shortDescription string
shortDescriptionFile string
fullDescription string
fullDescriptionFile string
semVersion *semver.Version
version string version string
flogger logger.Logger
) )
func init() {
// sVersion, err := semver.NewVersion(version)
// if err != nil {
// log.Fatalf("Can not create new semantic version from %v: %v", version, err)
// }
// semVersion = sVersion
flogger = logger.NewDefaultLogger(logger.LogLevelDebug)
}
func main() { func main() {
cmd.Execute(version)
flogger.Debug("Parse flags")
flag.StringVar(&dockerHubUser, "user", "", "Docker Hub Username")
flag.StringVar(&dockerHubPassword, "password", "", "Docker Hub Password")
flag.StringVar(&dockerHubNamespace, "namespace", "", "Docker Hub Namespace")
flag.StringVar(&dockerHubRepository, "repository", "", "Docker Hub Repository")
flag.StringVar(&shortDescription, "short-description", "", "Short description of the repository ")
flag.StringVar(&shortDescriptionFile, "short-description-file", "", "Short description of the repository. Override short-description if defined.")
flag.StringVar(&fullDescription, "full-description", "", "Full description of the repository")
flag.StringVar(&fullDescriptionFile, "full-description-file", "./README.md", "Full description of the repository. Override full-description if defined.")
flag.Parse()
if len(dockerHubUser) <= 0 {
flogger.Fatal("No user defined over flags")
}
if len(dockerHubPassword) <= 0 {
flogger.Fatal("No password defined over flags")
}
if len(dockerHubNamespace) <= 0 {
flogger.Fatal("No namespace defined over flags")
}
if len(dockerHubRepository) <= 0 {
flogger.Fatal("No repository defined over flags")
}
hub.SetLogger(flogger)
loginCredentials := &types.LoginCredentials{
User: dockerHubUser,
Password: dockerHubPassword,
}
actualShortDescription := ""
if len(shortDescription) > 0 {
actualShortDescription = shortDescription
flogger.Debug("Select short description from flag")
} else if len(shortDescriptionFile) > 0 {
f, err := ioutil.ReadFile(shortDescriptionFile)
if err != nil {
log.Fatalf("Can not read file %v", shortDescriptionFile)
}
actualShortDescription = string(f)
flogger.Debug("Select short description from file")
}
actualFullDescription := ""
if len(fullDescription) > 0 {
actualFullDescription = fullDescription
flogger.Debug("Select full description from flag")
} else if len(fullDescriptionFile) > 0 {
f, err := ioutil.ReadFile(fullDescriptionFile)
if err != nil {
log.Fatalf("Can not read file %v", fullDescriptionFile)
}
actualFullDescription = string(f)
flogger.Debug("Select full description from file")
}
flogger.Debug("Get Token")
token, err := hub.GetToken(loginCredentials)
if err != nil {
log.Fatalf("%v", err)
}
repository := &types.Repository{
Name: dockerHubRepository,
Namespcace: dockerHubNamespace,
Description: actualShortDescription,
FullDescription: actualFullDescription,
}
flogger.Debug("Send Repository Patch")
_, err = hub.PatchRepository(repository, token)
if err != nil {
log.Fatalf("%v", err)
}
} }