1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
// Copyright (c) 2017, Yannick Cote <yhcote@gmail.com> All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
// Package siftool adds siftool commands to a parent cobra.Command.
package siftool
import (
"github.com/spf13/cobra"
"github.com/sylabs/sif/v2/internal/app/siftool"
)
// command contains options and command state.
type command struct {
opts commandOpts
app *siftool.App
}
// initApp initializes the siftool app.
func (c *command) initApp(cmd *cobra.Command, _ []string) error {
app, err := siftool.New(
siftool.OptAppOutput(cmd.OutOrStdout()),
siftool.OptAppError(cmd.ErrOrStderr()),
)
c.app = app
return err
}
// commandOpts contains configured options.
type commandOpts struct {
rootPath string
experimental bool
}
// CommandOpt are used to configure optional command behavior.
type CommandOpt func(*commandOpts) error
// OptWithExperimental enables/disables experimental commands.
func OptWithExperimental(b bool) CommandOpt {
return func(co *commandOpts) error {
co.experimental = b
return nil
}
}
// AddCommands adds siftool commands to cmd according to opts.
//
// A set of commands are provided to display elements such as the SIF global
// header, the data object descriptors and to dump data objects. It is also
// possible to modify a SIF file via this tool via the add/del commands.
func AddCommands(cmd *cobra.Command, opts ...CommandOpt) error {
c := command{
opts: commandOpts{
rootPath: cmd.CommandPath(),
},
}
for _, opt := range opts {
if err := opt(&c.opts); err != nil {
return err
}
}
cmd.AddCommand(
c.getHeader(),
c.getList(),
c.getInfo(),
c.getDump(),
c.getNew(),
c.getAdd(),
c.getDel(),
c.getSetPrim(),
)
return nil
}
|