Skip to content

Commit

Permalink
Making swctl as library
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Béreš <daniel.beres@pantheon.tech>
  • Loading branch information
Giluerre committed Aug 23, 2023
1 parent c7dc515 commit 75dc091
Show file tree
Hide file tree
Showing 17 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cmd/swctl/all_imports.go → cmd/swctl/app/all_imports.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
_ "go.ligato.io/vpp-agent/v3/proto/ligato/linux"
Expand Down
6 changes: 5 additions & 1 deletion cmd/swctl/cli.go → cmd/swctl/app/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"errors"
Expand Down Expand Up @@ -76,6 +76,10 @@ func (cli *CLI) Initialize(opts *GlobalOptions) (err error) {

// load entity files
cli.entities, err = loadEntityFiles(opts.EntityFiles)
if cli.entities == nil {
cli.entities, err = loadEmbeddedEntities(opts.EmbeddedEntityByte)

}
if err != nil {
return fmt.Errorf("loading entity files failed: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/cli_options.go → cmd/swctl/app/cli_options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"io"
Expand Down
7 changes: 3 additions & 4 deletions cmd/swctl/cmd.go → cmd/swctl/app/cmd.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main
package app

import (
"fmt"

"github.com/gookit/color"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -57,7 +56,7 @@ func NewRootCmd(cli Cli) *cobra.Command {
cmd.InitDefaultHelpFlag()
cmd.Flags().Lookup("help").Hidden = true

cmd.AddCommand(newVersionCmd())
cmd.AddCommand(NewVersionCmd())
cmd.AddCommand(
NewConfigCmd(cli),
NewDeploymentCmd(cli),
Expand All @@ -77,7 +76,7 @@ func NewRootCmd(cli Cli) *cobra.Command {
return cmd
}

func newVersionCmd() *cobra.Command {
func NewVersionCmd() *cobra.Command {
var (
short bool
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/cmd_config.go → cmd/swctl/app/cmd_config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

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

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

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/cmd_status.go → cmd/swctl/app/cmd_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package main
package app

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

import (
"archive/zip"
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/cmd_trace.go → cmd/swctl/app/cmd_trace.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"fmt"
Expand Down
19 changes: 18 additions & 1 deletion cmd/swctl/entity.go → cmd/swctl/app/entity.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"fmt"
Expand Down Expand Up @@ -77,6 +77,23 @@ func loadEntityFiles(files []string) ([]Entity, error) {
return entities, nil
}

func loadEmbeddedEntities(embeddedEntities []byte) ([]Entity, error) {
var entities []Entity
var entityFile EntityFile
if err := yaml.Unmarshal(embeddedEntities, &entityFile); err != nil {
return nil, err
}
for _, entity := range entityFile.Entities {
if err := validateEntity(&entity); err != nil {
return nil, fmt.Errorf("invalid entity %v: %w", entity.Name, err)
}

logrus.Tracef("loaded entity: %v", entity.Name)
entities = append(entities, entity)
}
return entities, nil
}

// EntityVar is a variable of an entity defined with a template to render its value.
type EntityVar struct {
Index int `json:"-"`
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/exec.go → cmd/swctl/app/exec.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/formatter.go → cmd/swctl/app/formatter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/log.go → cmd/swctl/app/log.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"bytes"
Expand Down
7 changes: 4 additions & 3 deletions cmd/swctl/options.go → cmd/swctl/app/options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"os"
Expand All @@ -21,8 +21,9 @@ type GlobalOptions struct {
LogLevel string
Color string

ComposeFiles []string
EntityFiles []string
ComposeFiles []string
EntityFiles []string
EmbeddedEntityByte []byte

// TODO: support config file
// Config string
Expand Down
2 changes: 1 addition & 1 deletion cmd/swctl/util.go → cmd/swctl/app/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"archive/tar"
Expand Down
6 changes: 4 additions & 2 deletions cmd/swctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"github.com/sirupsen/logrus"

"go.pantheon.tech/stonework/cmd/swctl/app"
)

func main() {
Expand All @@ -10,12 +12,12 @@ func main() {

// Execute executes a root command using default behavior
func Execute() {
cli, err := NewCli()
cli, err := app.NewCli()
if err != nil {
logrus.Fatalf("ERROR: %v", err)
}

root := NewRootCmd(cli)
root := app.NewRootCmd(cli)

if err := root.Execute(); err != nil {
logrus.Fatalf("ERROR: %v", err)
Expand Down

0 comments on commit 75dc091

Please sign in to comment.