Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] refactor(cmd/*): move commands into packages #44

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/complete.go → cmd/complete/complete.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package complete

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/config.go → cmd/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cmd_config

import (
"flag"
Expand Down
27 changes: 15 additions & 12 deletions cmd/image.go → cmd/image/image.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cmd_image

import (
"bufio"
Expand All @@ -20,6 +20,8 @@ import (
"strconv"
"strings"

"github.com/belgaied2/harvester-cli/cmd/vm"
"github.com/belgaied2/harvester-cli/common"
"github.com/harvester/harvester/pkg/apis/harvesterhci.io/v1beta1"
rcmd "github.com/rancher/cli/cmd"
"github.com/rancher/cli/config"
Expand Down Expand Up @@ -61,6 +63,7 @@ const (

// TemplateCommand defines the CLI command that lists VM templates in Harvester
func ImageCommand() *cli.Command {
nsFlag := cmd_vm.NamespaceFlag
return &cli.Command{
Name: "image",
Aliases: []string{"img"},
Expand Down Expand Up @@ -89,7 +92,7 @@ func ImageCommand() *cli.Command {
ArgsUsage: "VM_IMAGE_DISPLAYNAME",
Action: imageCreate,
Flags: []cli.Flag{
&nsFlag,
&cmd_vm.NamespaceFlag,
&cli.StringFlag{
Name: "source",
Usage: "Location from which the image will be put into Harvester, this should be either an HTTP(S) link or a path to a file that harvester will use to get the image",
Expand All @@ -112,7 +115,7 @@ func ImageCommand() *cli.Command {
ArgsUsage: "",
Action: imageCatalog,
Flags: []cli.Flag{
&nsFlag,
&cmd_vm.NamespaceFlag,
&cli.StringFlag{
Name: "metadata-url",
Usage: "Location from which to get the metadata JSON file",
Expand All @@ -127,7 +130,7 @@ func ImageCommand() *cli.Command {
}

var (
ctxv1 = cliv1.NewContext(
Ctxv1 = cliv1.NewContext(
&cliv1.App{
Name: "harvester",
},
Expand All @@ -137,7 +140,7 @@ var (
)

func imageList(ctx *cli.Context) (err error) {
c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return
Expand All @@ -155,7 +158,7 @@ func imageList(ctx *cli.Context) (err error) {
{"SOURCE TYPE", "SourceType"},
{"URL", "Url"},
},
ctxv1)
Ctxv1)

defer writer.Close()

Expand Down Expand Up @@ -311,7 +314,7 @@ func createImageObjectInAPI(ctx *cli.Context, vmImageDisplayName string, sourceT
},
}

c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return
Expand Down Expand Up @@ -345,7 +348,7 @@ func getHarvesterAPIFromConfig(ctx *cli.Context) (serverConfig *config.ServerCon

harvesterKubeAPIServerHost := u.Host

tokenMap, configMap, err := GetRancherTokenMap(ctx)
tokenMap, configMap, err := common.GetRancherTokenMap(ctx)

if err != nil {
return
Expand Down Expand Up @@ -393,7 +396,7 @@ func imageCatalog(ctx *cli.Context) (err error) {
{"NAME", "Name"},
{"NUMBER OF IMAGES", "NumberOfImages"},
},
ctxv1)
Ctxv1)

osChoiceMap := make(map[int64]string)
var i int64 = 0
Expand All @@ -414,7 +417,7 @@ func imageCatalog(ctx *cli.Context) (err error) {

fmt.Println("Insert a number to select the image OS: ")
reader := bufio.NewReader(os.Stdin)
selection, err := GetSelectionFromInput(reader, len(osChoiceMap))
selection, err := common.GetSelectionFromInput(reader, len(osChoiceMap))
if err != nil {
return err
}
Expand All @@ -429,7 +432,7 @@ func imageCatalog(ctx *cli.Context) (err error) {
{"VERSION", "Version"},
{"BUILD", "Build"},
{"URL", "Url"},
}, ctxv1)
}, Ctxv1)

imageChoiceMap := make(map[int64]string)

Expand All @@ -442,7 +445,7 @@ func imageCatalog(ctx *cli.Context) (err error) {
writer.Close()

fmt.Printf("\nInsert a number to select an image to download: \n")
selection, err = GetSelectionFromInput(reader, len(imageChoiceMap))
selection, err = common.GetSelectionFromInput(reader, len(imageChoiceMap))
if err != nil {
return err
}
Expand Down
18 changes: 10 additions & 8 deletions cmd/import.go → cmd/import/import.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd
package cmd_import

import (
"context"
"encoding/json"
"fmt"
"strings"

cmd_image "github.com/belgaied2/harvester-cli/cmd/image"
"github.com/belgaied2/harvester-cli/common"
VMImportV1 "github.com/harvester/vm-import-controller/pkg/apis/migration.harvesterhci.io/v1beta1"
rcmd "github.com/rancher/cli/cmd"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -186,7 +188,7 @@ func importDeleteCommand() *cli.Command {
}

func enableVMImport(ctx *cli.Context) error {
c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return err
Expand All @@ -213,7 +215,7 @@ func enableVMImport(ctx *cli.Context) error {
// listVMImports lists all VM imports present in Harvester
func listVMImports(ctx *cli.Context) error {

c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return err
Expand All @@ -238,7 +240,7 @@ func listVMImports(ctx *cli.Context) error {
{"STATUS", "Status"},
{"SOURCE_CLUSTER", "SourceCluster"},
{"CLUSTER_TYPE", "ClusterType"},
}, ctxv1)
}, cmd_image.Ctxv1)

defer writer.Close()

Expand All @@ -261,7 +263,7 @@ func configureVMImport(ctx *cli.Context) error {
return fmt.Errorf("VM import name is required, only 1 argument is allowed")
}

c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return err
Expand Down Expand Up @@ -361,7 +363,7 @@ func deleteVMImportSource(ctx *cli.Context) error {
return fmt.Errorf("VM import name is required, only 1 argument is allowed")
}

c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return err
Expand Down Expand Up @@ -400,7 +402,7 @@ func createVMImport(ctx *cli.Context) error {
return fmt.Errorf("VM import name is required, only 1 argument is allowed")
}

c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return err
Expand Down Expand Up @@ -480,7 +482,7 @@ func deleteVMImport(ctx *cli.Context) error {
return fmt.Errorf("VM import name is required, only 1 argument is allowed")
}

c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return err
Expand Down
13 changes: 8 additions & 5 deletions cmd/keypair.go → cmd/keypair/keypair.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd
package cmd_keypair

import (
"context"
"time"

cmd_image "github.com/belgaied2/harvester-cli/cmd/image"
cmd_vm "github.com/belgaied2/harvester-cli/cmd/vm"
"github.com/belgaied2/harvester-cli/common"
rcmd "github.com/rancher/cli/cmd"
"github.com/urfave/cli/v2"
k8smetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -23,7 +26,7 @@ func KeypairCommand() *cli.Command {
Usage: "Manipulate SSH Keypairs",
Action: keypairList,
Flags: []cli.Flag{
&nsFlag,
&cmd_vm.NamespaceFlag,
},
Subcommands: cli.Commands{
&cli.Command{
Expand All @@ -34,15 +37,15 @@ func KeypairCommand() *cli.Command {
ArgsUsage: "None",
Action: keypairList,
Flags: []cli.Flag{
&nsFlag,
&cmd_vm.NamespaceFlag,
},
},
},
}
}

func keypairList(ctx *cli.Context) (err error) {
c, err := GetHarvesterClient(ctx)
c, err := common.GetHarvesterClient(ctx)

if err != nil {
return
Expand All @@ -59,7 +62,7 @@ func keypairList(ctx *cli.Context) (err error) {
{"FINGERPRINT", "Fingerprint"},
{"CREATION TIMESTAMP", "CreationTimestamp"},
},
ctxv1)
cmd_image.Ctxv1)

defer writer.Close()

Expand Down
29 changes: 16 additions & 13 deletions cmd/login.go → cmd/login/login.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package cmd_login

import (
"bufio"
Expand All @@ -13,6 +13,9 @@ import (
"path/filepath"
"strings"

cmd_config "github.com/belgaied2/harvester-cli/cmd/config"
cmd_image "github.com/belgaied2/harvester-cli/cmd/image"
"github.com/belgaied2/harvester-cli/common"
"github.com/sirupsen/logrus"

"github.com/grantae/certinfo"
Expand Down Expand Up @@ -81,7 +84,7 @@ func loginSetup(ctx *cli.Context) error {
return cli.ShowCommandHelp(ctx, "login")
}

cf, err := loadConfig(ctx)
cf, err := common.LoadConfig(ctx)
if err != nil {
return err
}
Expand All @@ -103,7 +106,7 @@ func loginSetup(ctx *cli.Context) error {
serverConfig.URL = u.String()

if ctx.String("token") != "" {
auth := SplitOnColon(ctx.String("token"))
auth := common.SplitOnColon(ctx.String("token"))
if len(auth) != 2 {
return errors.New("invalid token")
}
Expand All @@ -116,7 +119,7 @@ func loginSetup(ctx *cli.Context) error {
}

if ctx.String("cacert") != "" {
cert, err := loadAndVerifyCert(ctx.String("cacert"))
cert, err := common.LoadAndVerifyCert(ctx.String("cacert"))
if err != nil {
return err
}
Expand Down Expand Up @@ -161,7 +164,7 @@ func loginSetup(ctx *cli.Context) error {
return err
}

err = ctx.Set("cluster", SplitOnColon(proj)[0])
err = ctx.Set("cluster", common.SplitOnColon(proj)[0])

if err != nil {
return err
Expand All @@ -173,7 +176,7 @@ func loginSetup(ctx *cli.Context) error {
return err
}

err = GetConfig(ctx)
err = cmd_config.GetConfig(ctx)
if err != nil {
return err
}
Expand All @@ -186,19 +189,19 @@ func getProjectContext(ctx *cli.Context, c *cliclient.MasterClient) (string, err
if ctx.String("context") != "" {
context := ctx.String("context")
// Check if given context is in valid format
_, _, err := parseClusterAndProjectID(context)
_, _, err := common.ParseClusterAndProjectID(context)
if err != nil {
return "", fmt.Errorf("unable to parse context (%s). Please provide context as local:p-xxxxx, c-xxxxx:p-xxxxx, or c-xxxxx:project-xxxxx", context)
}
// Check if context exists
_, err = Lookup(c, context, "project")
_, err = common.Lookup(c, context, "project")
if err != nil {
return "", fmt.Errorf("unable to find context (%s). Make sure the context exists and you have permissions to use it. Error: %s", context, err)
}
return context, nil
}

projectCollection, err := c.ManagementClient.Project.List(defaultListOpts(ctx))
projectCollection, err := c.ManagementClient.Project.List(common.DefaultListOpts(ctx))
if err != nil {
return "", err
}
Expand Down Expand Up @@ -231,7 +234,7 @@ func getProjectContext(ctx *cli.Context, c *cliclient.MasterClient) (string, err
}
}

clusterNames, err := getClusterNames(ctx, c)
clusterNames, err := common.GetClusterNames(ctx, c)
if err != nil {
return "", err
}
Expand All @@ -242,7 +245,7 @@ func getProjectContext(ctx *cli.Context, c *cliclient.MasterClient) (string, err
{"PROJECT ID", "Project.ID"},
{"PROJECT NAME", "Project.Name"},
{"PROJECT DESCRIPTION", "Project.Description"},
}, ctxv1)
}, cmd_image.Ctxv1)

for i, item := range projectCollection.Data {
writer.Write(&LoginData{
Expand All @@ -262,7 +265,7 @@ func getProjectContext(ctx *cli.Context, c *cliclient.MasterClient) (string, err
reader := bufio.NewReader(os.Stdin)

var selection int
selection, err = GetSelectionFromInput(reader, len(projectCollection.Data))
selection, err = common.GetSelectionFromInput(reader, len(projectCollection.Data))

if err != nil {
return "", err
Expand Down Expand Up @@ -302,7 +305,7 @@ func getCertFromServer(ctx *cli.Context, cf *config.ServerConfig) (*cliclient.Ma
return nil, fmt.Errorf("unable to parse response from %s/v3/settings/cacerts\nError: %s\nResponse:\n%s", cf.URL, err, content)
}

cert, err := verifyCert([]byte(certReponse.Value))
cert, err := common.VerifyCert([]byte(certReponse.Value))
if err != nil {
return nil, err
}
Expand Down
Loading