You've already forked docker-hub-description-updater
refac(hub): pack functions behind a struct
This commit is contained in:
121
cmd/root.go
121
cmd/root.go
@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
@ -15,74 +16,72 @@ var (
|
||||
dockerHubUser string
|
||||
dockerHubPassword string
|
||||
dockerHubRepository string
|
||||
file string
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "dhdu",
|
||||
Short: "docker hub description updater (dhdu)",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
if len(dockerHubUser) <= 0 {
|
||||
log.Fatalf("No user defined over flags")
|
||||
}
|
||||
|
||||
if len(dockerHubPassword) <= 0 {
|
||||
log.Fatalf("No password defined over flags")
|
||||
}
|
||||
|
||||
if len(dockerHubNamespace) <= 0 {
|
||||
log.Printf("No namespace defined over flags: Use docker username %v instead", dockerHubUser)
|
||||
dockerHubNamespace = dockerHubUser
|
||||
}
|
||||
|
||||
if len(dockerHubRepository) <= 0 {
|
||||
log.Fatalf("No repository defined over flags")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(file); os.IsNotExist(err) && len(file) <= 0 {
|
||||
log.Fatalf("Can not find file: %v", file)
|
||||
}
|
||||
|
||||
f, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatalf("Can not read file %v: %v", file, err)
|
||||
}
|
||||
fullDescription := string(f)
|
||||
|
||||
loginCredentials := &types.LoginCredentials{
|
||||
User: dockerHubUser,
|
||||
Password: dockerHubPassword,
|
||||
}
|
||||
|
||||
token, err := hub.GetToken(loginCredentials)
|
||||
if err != nil {
|
||||
log.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
repository := &types.Repository{
|
||||
Name: dockerHubRepository,
|
||||
Namespcace: dockerHubNamespace,
|
||||
FullDescription: fullDescription,
|
||||
}
|
||||
|
||||
_, err = hub.PatchRepository(repository, token)
|
||||
if err != nil {
|
||||
log.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
// Execute a
|
||||
func Execute(version string) {
|
||||
rootCmd.Version = version
|
||||
|
||||
rootCmd := &cobra.Command{
|
||||
Use: "dhdu",
|
||||
Short: "docker hub description updater (dhdu)",
|
||||
RunE: runE,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Version: version,
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
func runE(cmd *cobra.Command, args []string) error {
|
||||
|
||||
file := args[0]
|
||||
|
||||
if len(dockerHubUser) <= 0 {
|
||||
return fmt.Errorf("No user defined over flags")
|
||||
}
|
||||
|
||||
if len(dockerHubPassword) <= 0 {
|
||||
return fmt.Errorf("No password defined over flags")
|
||||
}
|
||||
|
||||
if len(dockerHubNamespace) <= 0 {
|
||||
log.Printf("No namespace defined over flags: Use docker username %v instead", dockerHubUser)
|
||||
dockerHubNamespace = dockerHubUser
|
||||
}
|
||||
|
||||
if len(dockerHubRepository) <= 0 {
|
||||
return fmt.Errorf("No repository defined over flags")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(file); os.IsNotExist(err) && len(file) <= 0 {
|
||||
return fmt.Errorf("Can not find file: %v", file)
|
||||
}
|
||||
|
||||
f, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Can not read file %v: %v", file, err)
|
||||
}
|
||||
fullDescription := string(f)
|
||||
|
||||
loginCredentials := &types.LoginCredentials{
|
||||
User: dockerHubUser,
|
||||
Password: dockerHubPassword,
|
||||
}
|
||||
|
||||
h := hub.New(loginCredentials)
|
||||
|
||||
repository := &types.Repository{
|
||||
Name: dockerHubRepository,
|
||||
Namespcace: dockerHubNamespace,
|
||||
FullDescription: fullDescription,
|
||||
}
|
||||
|
||||
_, err = h.PatchRepository(repository)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user