github.com/Benchkram/bob@v0.0.0-20220321080157-7c8f3876e225/cli/cmd_inspect.go (about)

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/Benchkram/bob/bob"
    10  	"github.com/Benchkram/bob/pkg/boblog"
    11  	"github.com/Benchkram/bob/pkg/usererror"
    12  	"github.com/Benchkram/errz"
    13  	"github.com/logrusorgru/aurora"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var inspectArtifactId string
    18  
    19  func init() {
    20  
    21  	inspectArtifactCmd.Flags().StringVarP(&inspectArtifactId, "id", "",
    22  		inspectArtifactId, "inspect artifact with id")
    23  
    24  	inspectCmd.AddCommand(envCmd)
    25  	inspectCmd.AddCommand(exportCmd)
    26  	inspectArtifactCmd.AddCommand(inspectArtifactListCmd)
    27  	inspectCmd.AddCommand(inspectArtifactCmd)
    28  	rootCmd.AddCommand(inspectCmd)
    29  }
    30  
    31  var inspectCmd = &cobra.Command{
    32  	Use:   "inspect",
    33  	Short: "Inspect Tasks of a Bobfile",
    34  	Long:  ``,
    35  	Run: func(cmd *cobra.Command, args []string) {
    36  		_ = cmd.Help()
    37  	},
    38  }
    39  
    40  var envCmd = &cobra.Command{
    41  	Use:   "env",
    42  	Short: "List environment for a task",
    43  	Args:  cobra.ExactArgs(1),
    44  	Long:  ``,
    45  	Run: func(cmd *cobra.Command, args []string) {
    46  		taskname := args[0]
    47  		runEnv(taskname)
    48  	},
    49  	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    50  		tasks, err := getTasks()
    51  		if err != nil {
    52  			return nil, cobra.ShellCompDirectiveError
    53  		}
    54  
    55  		return tasks, cobra.ShellCompDirectiveDefault
    56  	},
    57  }
    58  
    59  func runEnv(taskname string) {
    60  	b, err := bob.Bob()
    61  	boblog.Log.Error(err, "Unable to initialise bob")
    62  
    63  	bobfile, err := b.Aggregate()
    64  	boblog.Log.Error(err, "Unable to aggregate bob file")
    65  
    66  	task, ok := bobfile.BTasks[taskname]
    67  	if !ok {
    68  		fmt.Printf("%s\n", aurora.Red("Task does not exists"))
    69  		os.Exit(1)
    70  	}
    71  
    72  	for _, env := range task.Env() {
    73  		println(env)
    74  	}
    75  }
    76  
    77  var exportCmd = &cobra.Command{
    78  	Use:   "export",
    79  	Short: "List exports for a task",
    80  	Args:  cobra.ExactArgs(1),
    81  	Long:  ``,
    82  	Run: func(cmd *cobra.Command, args []string) {
    83  		taskname := args[0]
    84  		runExport(taskname)
    85  	},
    86  	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    87  		tasks, err := getTasks()
    88  		if err != nil {
    89  			return nil, cobra.ShellCompDirectiveError
    90  		}
    91  
    92  		return tasks, cobra.ShellCompDirectiveDefault
    93  	},
    94  }
    95  
    96  func runExport(taskname string) {
    97  	b, err := bob.Bob()
    98  	boblog.Log.Error(err, "Unable to initialize bob")
    99  
   100  	bobfile, err := b.Aggregate()
   101  	boblog.Log.Error(err, "Unable to aggregate bob file")
   102  
   103  	task, ok := bobfile.BTasks[taskname]
   104  	if !ok {
   105  		fmt.Printf("%s\n", aurora.Red("Task does not exists"))
   106  		os.Exit(1)
   107  	}
   108  
   109  	for exportname, export := range task.GetExports() {
   110  		fmt.Printf("%s (%s)\n", exportname, export)
   111  	}
   112  }
   113  
   114  var inspectArtifactCmd = &cobra.Command{
   115  	Use:   "artifact",
   116  	Short: "Inspect artifacts by id",
   117  	//Args:  cobra.ExactArgs(1),
   118  	Long: ``,
   119  	Run: func(cmd *cobra.Command, args []string) {
   120  		if inspectArtifactId == "" {
   121  			fmt.Printf("%s", aurora.Red("failed to set artifact id"))
   122  			os.Exit(1)
   123  		}
   124  		runInspectArtifact(inspectArtifactId)
   125  	},
   126  	ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   127  		tasks, err := getTasks()
   128  		if err != nil {
   129  			return nil, cobra.ShellCompDirectiveError
   130  		}
   131  
   132  		return tasks, cobra.ShellCompDirectiveDefault
   133  	},
   134  }
   135  
   136  func runInspectArtifact(artifactID string) {
   137  	b, err := bob.Bob()
   138  	boblog.Log.Error(err, "Unable to initialize bob")
   139  
   140  	info, err := b.ArtifactInspect(artifactID)
   141  	if err != nil {
   142  		if errors.As(err, &usererror.Err) {
   143  			fmt.Printf("%s\n", errors.Unwrap(err).Error())
   144  			os.Exit(1)
   145  		}
   146  		errz.Log(err)
   147  	}
   148  
   149  	fmt.Printf("%s", info.String())
   150  }
   151  
   152  var inspectArtifactListCmd = &cobra.Command{
   153  	Use:   "ls",
   154  	Short: "List artifacts",
   155  	Long:  ``,
   156  	Run: func(cmd *cobra.Command, args []string) {
   157  		runInspectArtifactList()
   158  	},
   159  }
   160  
   161  // runinspectArtifactList list artifacts in relation to tasks
   162  func runInspectArtifactList() {
   163  	b, err := bob.Bob()
   164  	if err != nil {
   165  		boblog.Log.Error(err, "Unable to initialize bob")
   166  	}
   167  
   168  	out, err := b.ArtifactList(context.TODO())
   169  	if err != nil {
   170  		boblog.Log.Error(err, "Unable to generate artifact list")
   171  	}
   172  	fmt.Println(out)
   173  }