corso/src/cli/utils/utils.go
Keepers 6cdc691c6f
correct the corso module (#749)
swaps the corso go module from github.com/
alcionai/corso to github.com/alcionai/corso/src
to align with the location of the go.mod and
go.sum files inside the repo.

All other changes in the repository update the
package imports to the new module path.
2022-09-07 15:50:54 -06:00

57 lines
1.3 KiB
Go

package utils
import (
"context"
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/alcionai/corso/src/pkg/repository"
)
const (
Wildcard = "*"
)
// RequireProps validates the existence of the properties
// in the map. Expects the format map[propName]propVal.
func RequireProps(props map[string]string) error {
for name, val := range props {
if len(val) == 0 {
return errors.New(name + " is required to perform this command")
}
}
return nil
}
// CloseRepo handles closing a repo.
func CloseRepo(ctx context.Context, r *repository.Repository) {
if err := r.Close(ctx); err != nil {
fmt.Print("Error closing repository:", err)
}
}
// HasNoFlagsAndShownHelp shows the Help output if no flags
// were provided to the command. Returns true if the help
// was shown.
// Use for when the non-flagged usage of a command
// (ex: corso backup restore exchange) is expected to no-op.
func HasNoFlagsAndShownHelp(cmd *cobra.Command) bool {
if cmd.Flags().NFlag() == 0 {
cobra.CheckErr(cmd.Help())
return true
}
return false
}
// AddCommand adds a clone of the subCommand to the parent,
// and returns both the clone and its pflags.
func AddCommand(parent, c *cobra.Command) (*cobra.Command, *pflag.FlagSet) {
parent.AddCommand(c)
return c, c.Flags()
}