Skip to content

Commit

Permalink
Add lsmeta and addmeta
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Mar 29, 2024
1 parent f52b752 commit fdf0f12
Show file tree
Hide file tree
Showing 33 changed files with 491 additions and 66 deletions.
13 changes: 10 additions & 3 deletions cmd/flag/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,25 @@ var (
commonFlagValues CommonFlagValues
)

func SetCommonFlags(command *cobra.Command) {
func SetCommonFlags(command *cobra.Command, noResource bool) {
command.Flags().StringVarP(&commonFlagValues.ConfigFilePath, "config", "c", "", "Set config file or dir (default \"$HOME/.irods\")")
command.Flags().BoolVarP(&commonFlagValues.ShowVersion, "version", "v", false, "Print version")
command.Flags().BoolVarP(&commonFlagValues.ShowHelp, "help", "h", false, "Print help")
command.Flags().BoolVarP(&commonFlagValues.DebugMode, "debug", "d", false, "Enable debug mode")
command.Flags().StringVar(&commonFlagValues.logLevelInput, "log_level", "", "Set log level")
command.Flags().IntVarP(&commonFlagValues.SessionID, "session", "s", os.Getppid(), "Set session ID")
command.Flags().StringVarP(&commonFlagValues.Resource, "resource", "R", "", "Set resource server")

if !noResource {
command.Flags().StringVarP(&commonFlagValues.Resource, "resource", "R", "", "Set resource server")
}

command.MarkFlagsMutuallyExclusive("debug", "version")
command.MarkFlagsMutuallyExclusive("log_level", "version")
command.MarkFlagsMutuallyExclusive("resource", "version")

if !noResource {
command.MarkFlagsMutuallyExclusive("resource", "version")
}

command.MarkFlagsMutuallyExclusive("session", "version")
}

Expand Down
42 changes: 42 additions & 0 deletions cmd/flag/target_object.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package flag

import (
"github.com/spf13/cobra"
)

type TargetObjectFlagValues struct {
PathUpdated bool
Path string
ResourceUpdated bool
Resource string
UserUpdated bool
User string
}

var (
targetObjectFlagValues TargetObjectFlagValues
)

func SetTargetObjectFlags(command *cobra.Command) {
command.Flags().StringVarP(&targetObjectFlagValues.Path, "path", "P", "", "Set a data object or collection as a target")
command.Flags().StringVarP(&targetObjectFlagValues.Resource, "resource", "R", "", "Set a resource as a target")
command.Flags().StringVarP(&targetObjectFlagValues.User, "user", "U", "", "Set a user as a target")

command.MarkFlagsMutuallyExclusive("path", "resource", "user")
}

func GetTargetObjectFlagValues(command *cobra.Command) *TargetObjectFlagValues {
if command.Flags().Changed("path") {
targetObjectFlagValues.PathUpdated = true
}

if command.Flags().Changed("resource") {
targetObjectFlagValues.ResourceUpdated = true
}

if command.Flags().Changed("user") {
targetObjectFlagValues.UserUpdated = true
}

return &targetObjectFlagValues
}
4 changes: 3 additions & 1 deletion cmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func main() {
})

// attach common flags
flag.SetCommonFlags(rootCmd)
flag.SetCommonFlags(rootCmd, true)

// add sub commands
subcmd.AddInitCommand(rootCmd)
Expand All @@ -89,6 +89,8 @@ func main() {
subcmd.AddBputCommand(rootCmd)
subcmd.AddSvrinfoCommand(rootCmd)
subcmd.AddPsCommand(rootCmd)
subcmd.AddLsmetaCommand(rootCmd)
subcmd.AddAddmetaCommand(rootCmd)
subcmd.AddCopySftpIdCommand(rootCmd)
subcmd.AddLsticketCommand(rootCmd)
subcmd.AddRmticketCommand(rootCmd)
Expand Down
139 changes: 139 additions & 0 deletions cmd/subcmd/addmeta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package subcmd

import (
irodsclient_fs "github.com/cyverse/go-irodsclient/fs"
"github.com/cyverse/gocommands/cmd/flag"
"github.com/cyverse/gocommands/commons"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
)

var addmetaCmd = &cobra.Command{
Use: "addmeta [attribute name] [attribute value] [attribute unit (optional)]",
Aliases: []string{"add_meta", "add_metadata"},
Short: "Add a metadata",
Long: `This adds a metadata to the given collection, data object, user, or a resource.`,
RunE: processAddmetaCommand,
Args: cobra.RangeArgs(2, 3),
}

func AddAddmetaCommand(rootCmd *cobra.Command) {
// attach common flags
flag.SetCommonFlags(addmetaCmd, true)

flag.SetTargetObjectFlags(addmetaCmd)

rootCmd.AddCommand(addmetaCmd)
}

func processAddmetaCommand(command *cobra.Command, args []string) error {
cont, err := flag.ProcessCommonFlags(command)
if err != nil {
return xerrors.Errorf("failed to process common flags: %w", err)
}

if !cont {
return nil
}

// handle local flags
_, err = commons.InputMissingFields()
if err != nil {
return xerrors.Errorf("failed to input missing fields: %w", err)
}

targetObjectFlagValues := flag.GetTargetObjectFlagValues(command)

// Create a connection
account := commons.GetAccount()
filesystem, err := commons.GetIRODSFSClient(account)
if err != nil {
return xerrors.Errorf("failed to get iRODS FS Client: %w", err)
}

defer filesystem.Release()

// get avu
attr := args[0]
value := args[1]
unit := ""
if len(args) >= 3 {
unit = args[2]
}

if targetObjectFlagValues.PathUpdated {
err = addMetaToPath(filesystem, targetObjectFlagValues.Path, attr, value, unit)
if err != nil {
return err
}
} else if targetObjectFlagValues.UserUpdated {
err = addMetaToUser(filesystem, targetObjectFlagValues.User, attr, value, unit)
if err != nil {
return err
}
} else if targetObjectFlagValues.ResourceUpdated {
err = addMetaToResource(filesystem, targetObjectFlagValues.Resource, attr, value, unit)
if err != nil {
return err
}
} else {
// nothing updated
return xerrors.Errorf("path, user, or resource must be given")
}

return nil
}

func addMetaToPath(fs *irodsclient_fs.FileSystem, targetPath string, attribute string, value string, unit string) error {
logger := log.WithFields(log.Fields{
"package": "subcmd",
"function": "addMetaToPath",
})

logger.Debugf("add metadata to path %s (attr %s, value %s, unit %s)", targetPath, attribute, value, unit)

cwd := commons.GetCWD()
home := commons.GetHomeDir()
zone := commons.GetZone()
targetPath = commons.MakeIRODSPath(cwd, home, zone, targetPath)

err := fs.AddMetadata(targetPath, attribute, value, unit)
if err != nil {
return xerrors.Errorf("failed to add metadata to path %s (attr %s, value %s, unit %s): %w", targetPath, attribute, value, unit, err)
}

return nil
}

func addMetaToUser(fs *irodsclient_fs.FileSystem, username string, attribute string, value string, unit string) error {
logger := log.WithFields(log.Fields{
"package": "subcmd",
"function": "addMetaToUser",
})

logger.Debugf("add metadata to user %s (attr %s, value %s, unit %s)", username, attribute, value, unit)

err := fs.AddUserMetadata(username, attribute, value, unit)
if err != nil {
return xerrors.Errorf("failed to add metadata to user %s (attr %s, value %s, unit %s): %w", username, attribute, value, unit, err)
}

return nil
}

func addMetaToResource(fs *irodsclient_fs.FileSystem, resource string, attribute string, value string, unit string) error {
logger := log.WithFields(log.Fields{
"package": "subcmd",
"function": "addMetaToResource",
})

logger.Debugf("add metadata to resource %s (attr %s, value %s, unit %s)", resource, attribute, value, unit)

err := fs.AddUserMetadata(resource, attribute, value, unit)
if err != nil {
return xerrors.Errorf("failed to add metadata to resource %s (attr %s, value %s, unit %s): %w", resource, attribute, value, unit, err)
}

return nil
}
6 changes: 3 additions & 3 deletions cmd/subcmd/bclean.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var bcleanCmd = &cobra.Command{

func AddBcleanCommand(rootCmd *cobra.Command) {
// attach common flags
flag.SetCommonFlags(bcleanCmd)
flag.SetCommonFlags(bcleanCmd, false)

// attach bundle temp flags
flag.SetBundleTempFlags(bcleanCmd)
Expand All @@ -30,7 +30,7 @@ func AddBcleanCommand(rootCmd *cobra.Command) {

func processBcleanCommand(command *cobra.Command, args []string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "processBcleanCommand",
})

Expand Down Expand Up @@ -82,7 +82,7 @@ func processBcleanCommand(command *cobra.Command, args []string) error {

func bcleanOne(fs *irodsclient_fs.FileSystem, targetPath string, forceFlagValues *flag.ForceFlagValues) {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "bcleanOne",
})

Expand Down
8 changes: 4 additions & 4 deletions cmd/subcmd/bput.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var bputCmd = &cobra.Command{

func AddBputCommand(rootCmd *cobra.Command) {
// attach common flags
flag.SetCommonFlags(bputCmd)
flag.SetCommonFlags(bputCmd, false)

flag.SetBundleTempFlags(bputCmd)
flag.SetBundleClearFlags(bputCmd)
Expand All @@ -42,7 +42,7 @@ func AddBputCommand(rootCmd *cobra.Command) {

func processBputCommand(command *cobra.Command, args []string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "processBputCommand",
})

Expand Down Expand Up @@ -197,7 +197,7 @@ func processBputCommand(command *cobra.Command, args []string) error {

func bputOne(bundleManager *commons.BundleTransferManager, sourcePath string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "bputOne",
})

Expand Down Expand Up @@ -253,7 +253,7 @@ func bputDeleteExtra(bundleManager *commons.BundleTransferManager, targetPath st

func bputDeleteExtraInternal(filesystem *irodsclient_fs.FileSystem, inputPathMap map[string]bool, targetPath string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "bputDeleteExtraInternal",
})

Expand Down
4 changes: 2 additions & 2 deletions cmd/subcmd/bun.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var bunCmd = &cobra.Command{

func AddBunCommand(rootCmd *cobra.Command) {
// attach common flags
flag.SetCommonFlags(bunCmd)
flag.SetCommonFlags(bunCmd, false)

flag.SetForceFlags(bunCmd, false)
flag.SetBundleFlags(bunCmd)
Expand Down Expand Up @@ -110,7 +110,7 @@ func getDataType(irodsPath string, dataType string) (irodsclient_types.DataType,

func extractOne(filesystem *irodsclient_fs.FileSystem, sourcePath string, targetPath string, bundleFlagValues *flag.BundleFlagValues, forceFlagValues *flag.ForceFlagValues) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "extractOne",
})

Expand Down
6 changes: 3 additions & 3 deletions cmd/subcmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var catCmd = &cobra.Command{

func AddCatCommand(rootCmd *cobra.Command) {
// attach common flags
flag.SetCommonFlags(catCmd)
flag.SetCommonFlags(catCmd, false)

flag.SetTicketAccessFlags(catCmd)

Expand All @@ -32,7 +32,7 @@ func AddCatCommand(rootCmd *cobra.Command) {

func processCatCommand(command *cobra.Command, args []string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "processCatCommand",
})

Expand Down Expand Up @@ -88,7 +88,7 @@ func processCatCommand(command *cobra.Command, args []string) error {

func catOne(filesystem *irodsclient_fs.FileSystem, targetPath string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "catOne",
})

Expand Down
4 changes: 2 additions & 2 deletions cmd/subcmd/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var cdCmd = &cobra.Command{

func AddCdCommand(rootCmd *cobra.Command) {
// attach common flags
flag.SetCommonFlags(cdCmd)
flag.SetCommonFlags(cdCmd, true)

rootCmd.AddCommand(cdCmd)
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func processCdCommand(command *cobra.Command, args []string) error {

func changeWorkingDir(fs *irodsclient_fs.FileSystem, collectionPath string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "changeWorkingDir",
})

Expand Down
4 changes: 2 additions & 2 deletions cmd/subcmd/copy-sftp-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var copySftpIdCmd = &cobra.Command{

func AddCopySftpIdCommand(rootCmd *cobra.Command) {
// attach common flags
flag.SetCommonFlags(copySftpIdCmd)
flag.SetCommonFlags(copySftpIdCmd, false)

flag.SetForceFlags(copySftpIdCmd, false)
flag.SetDryRunFlags(copySftpIdCmd)
Expand Down Expand Up @@ -122,7 +122,7 @@ func scanSSHIdentityFiles() ([]string, error) {

func copySftpId(filesystem *irodsclient_fs.FileSystem, forceFlagValues *flag.ForceFlagValues, dryRunFlagValues *flag.DryRunFlagValues, identityFiles []string) error {
logger := log.WithFields(log.Fields{
"package": "main",
"package": "subcmd",
"function": "copySftpId",
})

Expand Down
Loading

0 comments on commit fdf0f12

Please sign in to comment.