Skip to content

Commit

Permalink
Collect all output into one package for more consistent handling
Browse files Browse the repository at this point in the history
Add pkg/output package to control logging and text output while running
commands. Package includes a debug log level, which can be enabled by
using the global flag '-v|--verbose'.

Add verbose logging to commands where appropriate and slim down
non-verbose log
  • Loading branch information
amisevsk committed Feb 23, 2024
1 parent cba25d4 commit 26d36f8
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 56 deletions.
11 changes: 8 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"kitops/pkg/cmd/push"
"kitops/pkg/cmd/version"
"kitops/pkg/lib/constants"
"kitops/pkg/output"

"github.com/spf13/cobra"
)
Expand All @@ -29,6 +30,7 @@ var (

type rootFlags struct {
configHome string
verbose bool
}

func RunCommand() *cobra.Command {
Expand All @@ -43,18 +45,21 @@ func RunCommand() *cobra.Command {
if configHome == "" {
currentUser, err := user.Current()
if err != nil {
fmt.Printf("Failed to resolve default storage path '$HOME/%s: could not get current user", constants.DefaultConfigSubdir)
output.Fatalf("Failed to resolve default storage path '$HOME/%s: could not get current user", constants.DefaultConfigSubdir)
}
configHome = filepath.Join(currentUser.HomeDir, constants.DefaultConfigSubdir)
}
if flags.verbose {
output.SetDebug(true)
}
ctx := context.WithValue(cmd.Context(), constants.ConfigKey{}, configHome)
cmd.SetContext(ctx)
},
}

addSubcommands(cmd)
cmd.PersistentFlags().StringVar(&flags.configHome, "config", "", fmt.Sprintf("config file (default is $HOME/%s)", constants.DefaultConfigSubdir))

cmd.PersistentFlags().StringVar(&flags.configHome, "config", "", fmt.Sprintf("Config file (default $HOME/%s)", constants.DefaultConfigSubdir))
cmd.PersistentFlags().BoolVarP(&flags.verbose, "verbose", "v", false, "Include additional information in output (default false)")
return cmd
}

Expand Down
8 changes: 3 additions & 5 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package build

import (
"fmt"
"kitops/pkg/artifact"
"kitops/pkg/lib/constants"
"kitops/pkg/lib/filesystem"
"kitops/pkg/lib/storage"
"kitops/pkg/output"
"os"
"path"
)

func RunBuild(options *buildOptions) error {
fmt.Println("build called")
// 1. Read the model file
modelfile, err := os.Open(options.modelFile)
if err != nil {
Expand All @@ -20,7 +19,6 @@ func RunBuild(options *buildOptions) error {
defer modelfile.Close()
kitfile := &artifact.KitFile{}
if err = kitfile.LoadModel(modelfile); err != nil {
fmt.Println(err)
return err
}

Expand All @@ -39,6 +37,7 @@ func RunBuild(options *buildOptions) error {
}
model.Layers = append(model.Layers, *layer)
}

// 3. package the DataSets
for _, dataset := range kitfile.DataSets {
datasetPath, err := filesystem.VerifySubpath(options.contextDir, dataset.Path)
Expand Down Expand Up @@ -73,7 +72,6 @@ func RunBuild(options *buildOptions) error {
store := storage.NewLocalStore(modelStorePath, repo)
manifestDesc, err := store.SaveModel(model, tag)
if err != nil {
fmt.Println(err)
return err
}

Expand All @@ -83,7 +81,7 @@ func RunBuild(options *buildOptions) error {
}
}

fmt.Println("Model saved: ", manifestDesc.Digest)
output.Infof("Model saved: %s", manifestDesc.Digest)

return nil
}
21 changes: 19 additions & 2 deletions pkg/cmd/build/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"context"
"fmt"
"path"
"strings"

"kitops/pkg/lib/constants"
"kitops/pkg/lib/storage"
"kitops/pkg/output"

"github.com/spf13/cobra"
"oras.land/oras-go/v2/registry"
Expand Down Expand Up @@ -55,12 +57,12 @@ func runCommand(flags *buildFlags) func(cmd *cobra.Command, args []string) {
opts := &buildOptions{}
err := opts.complete(cmd.Context(), flags, args)
if err != nil {
fmt.Println(err)
output.Fatalf("Failed to process configuration: %s", err)
return
}
err = RunBuild(opts)
if err != nil {
fmt.Println(err)
output.Fatalf("Failed to build model kit: %s", err)
return
}
}
Expand Down Expand Up @@ -89,5 +91,20 @@ func (opts *buildOptions) complete(ctx context.Context, flags *buildFlags, args
opts.modelRef = modelRef
opts.extraRefs = extraRefs
}
printConfig(opts)
return nil
}

func printConfig(opts *buildOptions) {
output.Debugf("Using storage path: %s", opts.storageHome)
output.Debugf("Context dir: %s", opts.contextDir)
output.Debugf("Model file: %s", opts.modelFile)
if opts.modelRef != nil {
output.Debugf("Building %s", opts.modelRef.String())
} else {
output.Debugln("No tag or reference specified")
}
if len(opts.extraRefs) > 0 {
output.Debugf("Additional tags: %s", strings.Join(opts.extraRefs, ", "))
}
}
19 changes: 12 additions & 7 deletions pkg/cmd/export/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"kitops/pkg/lib/constants"
"kitops/pkg/lib/storage"
"kitops/pkg/output"

"github.com/spf13/cobra"
"oras.land/oras-go/v2"
Expand Down Expand Up @@ -78,6 +79,7 @@ func (opts *exportOptions) complete(ctx context.Context, flags *exportFlags, arg
opts.exportConf.exportDatasets = flags.exportDatasets
}

printConfig(opts)
return nil
}

Expand Down Expand Up @@ -107,25 +109,22 @@ func runCommand(flags *exportFlags) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
opts := &exportOptions{}
if err := opts.complete(cmd.Context(), flags, args); err != nil {
fmt.Printf("Failed to process arguments: %s", err)
return
output.Fatalf("Failed to process arguments: %s", err)
}

store, err := getStoreForRef(cmd.Context(), opts)
if err != nil {
fmt.Println(err)
return
output.Fatalln(err)
}

exportTo := opts.exportDir
if exportTo == "" {
exportTo = "current directory"
}
fmt.Printf("Exporting to %s\n", exportTo)
output.Infof("Exporting to %s", exportTo)
err = exportModel(cmd.Context(), store, opts.modelRef, opts)
if err != nil {
fmt.Println(err)
return
output.Fatalln(err)
}
}
}
Expand Down Expand Up @@ -163,3 +162,9 @@ func getStoreForRef(ctx context.Context, opts *exportOptions) (oras.Target, erro

return repo, nil
}

func printConfig(opts *exportOptions) {
output.Debugf("Using storage path: %s", opts.storageHome)
output.Debugf("Overwrite: %t", opts.overwrite)
output.Debugf("Exporting %s", opts.modelRef.String())
}
18 changes: 11 additions & 7 deletions pkg/cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"kitops/pkg/lib/constants"
"kitops/pkg/lib/filesystem"
"kitops/pkg/lib/repo"
"kitops/pkg/output"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -49,15 +50,15 @@ func exportModel(ctx context.Context, store oras.Target, ref *registry.Reference
}
modelEntry := config.Model
layerDir = filepath.Join(options.exportDir, modelEntry.Path)
fmt.Printf("Exporting model %s to %s\n", modelEntry.Name, layerDir)
output.Infof("Exporting model %s to %s", modelEntry.Name, layerDir)

case constants.CodeLayerMediaType:
if !options.exportConf.exportCode {
continue
}
codeEntry := config.Code[codeIdx]
layerDir = filepath.Join(options.exportDir, codeEntry.Path)
fmt.Printf("Exporting code to %s\n", layerDir)
output.Infof("Exporting code to %s", layerDir)
codeIdx += 1

case constants.DataSetLayerMediaType:
Expand All @@ -66,13 +67,15 @@ func exportModel(ctx context.Context, store oras.Target, ref *registry.Reference
}
datasetEntry := config.DataSets[datasetIdx]
layerDir = filepath.Join(options.exportDir, datasetEntry.Path)
fmt.Printf("Exporting dataset %s to %s\n", datasetEntry.Name, layerDir)
output.Infof("Exporting dataset %s to %s", datasetEntry.Name, layerDir)
datasetIdx += 1
}
if err := exportLayer(ctx, store, layerDesc, layerDir, options.overwrite); err != nil {
return err
}
}
output.Debugf("Exported %d code layers", codeIdx)
output.Debugf("Exported %d dataset layers", datasetIdx)

return nil
}
Expand All @@ -92,7 +95,7 @@ func exportConfig(config *artifact.KitFile, exportDir string, overwrite bool) er
return fmt.Errorf("failed to export config: %w", err)
}

fmt.Printf("Exporting config to %s\n", configPath)
output.Infof("Exporting config to %s", configPath)
if err := os.WriteFile(configPath, configBytes, 0644); err != nil {
return fmt.Errorf("failed to write config file: %w", err)
}
Expand All @@ -119,6 +122,7 @@ func exportLayer(ctx context.Context, store content.Storage, desc ocispec.Descri
} else if !fi.IsDir() {
return fmt.Errorf("failed to export: path %s exists and is not a directory", exportDir)
}
output.Debugf("Directory %s already exists", exportDir)
}
if err := os.MkdirAll(exportDir, 0755); err != nil {
return fmt.Errorf("failed to create directory %s: %w", exportDir, err)
Expand All @@ -137,7 +141,6 @@ func extractTar(tr *tar.Reader, dir string, overwrite bool) error {
return err
}
outPath := path.Join(dir, header.Name)
fmt.Printf("Extracting %s\n", outPath)

switch header.Typeflag {
case tar.TypeDir:
Expand All @@ -148,8 +151,9 @@ func extractTar(tr *tar.Reader, dir string, overwrite bool) error {
if !fi.IsDir() {
return fmt.Errorf("path '%s' already exists and is not a directory", outPath)
}
output.Debugf("Path %s already exists", outPath)
}
fmt.Printf("Creating directory %s\n", outPath)
output.Debugf("Creating directory %s", outPath)
if err := os.MkdirAll(outPath, header.FileInfo().Mode()); err != nil {
return fmt.Errorf("failed to create directory %s: %w", outPath, err)
}
Expand All @@ -163,7 +167,7 @@ func extractTar(tr *tar.Reader, dir string, overwrite bool) error {
return fmt.Errorf("path '%s' already exists and is not a regular file", outPath)
}
}
fmt.Printf("Extracting file %s\n", outPath)
output.Debugf("Extracting file %s", outPath)
file, err := os.OpenFile(outPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, header.FileInfo().Mode())
if err != nil {
return fmt.Errorf("failed to create file %s: %w", outPath, err)
Expand Down
18 changes: 11 additions & 7 deletions pkg/cmd/list/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"kitops/pkg/lib/constants"
"kitops/pkg/lib/storage"
"kitops/pkg/output"
"os"
"path"
"text/tabwriter"
Expand Down Expand Up @@ -70,29 +71,25 @@ func runCommand(flags *listFlags) func(*cobra.Command, []string) {
return func(cmd *cobra.Command, args []string) {
opts := &listOptions{}
if err := opts.complete(cmd.Context(), flags, args); err != nil {
fmt.Printf("Failed to parse argument: %s", err)
return
output.Fatalf("Failed to parse argument: %s", err)
}

var allInfoLines []string
if opts.remoteRef == nil {
lines, err := listLocalKits(opts.storageHome)
if err != nil {
fmt.Println(err)
return
output.Fatalln(err)
}
allInfoLines = lines
} else {
lines, err := listRemoteKits(cmd.Context(), opts.remoteRef, opts.usehttp)
if err != nil {
fmt.Println(err)
return
output.Fatalln(err)
}
allInfoLines = lines
}

printSummary(allInfoLines)

}
}

Expand All @@ -104,3 +101,10 @@ func printSummary(lines []string) {
}
tw.Flush()
}

func printConfig(opts *listOptions) {
output.Debugf("Using storage path: %s", opts.storageHome)
if opts.remoteRef != nil {
output.Debugf("Listing remote model kits in %s", opts.remoteRef.String())
}
}
4 changes: 2 additions & 2 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Copyright © 2024 Jozu.com
package login

import (
"fmt"
"kitops/pkg/output"

"github.com/spf13/cobra"
)
Expand All @@ -20,7 +20,7 @@ func LoginCommand() *cobra.Command {
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("login called")
output.Infoln("login called")
},
}

Expand Down
Loading

0 comments on commit 26d36f8

Please sign in to comment.