diff --git a/Makefile b/Makefile index 4dc0073..56af4bb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PKG=github.com/cyverse/gocommands -VERSION=v0.1.0 +VERSION=v0.2.0 GIT_COMMIT?=$(shell git rev-parse HEAD) BUILD_DATE?=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ") LDFLAGS?="-X '${PKG}/commons.clientVersion=${VERSION}' -X '${PKG}/commons.gitCommit=${GIT_COMMIT}' -X '${PKG}/commons.buildDate=${BUILD_DATE}'" @@ -12,75 +12,71 @@ GOPATH=$(shell go env GOPATH) .PHONY: build build: mkdir -p bin - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/goinit ./cmd/goinit/goinit.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gols ./cmd/gols/gols.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/goget ./cmd/goget/goget.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/goput ./cmd/goput/goput.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gocd ./cmd/gocd/gocd.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gopwd ./cmd/gopwd/gopwd.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gomv ./cmd/gomv/gomv.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gocp ./cmd/gocp/gocp.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gorm ./cmd/gorm/gorm.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gormdir ./cmd/gormdir/gormdir.go - CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gomkdir ./cmd/gomkdir/gomkdir.go - + CGO_ENABLED=0 go build -ldflags=${LDFLAGS} -o bin/gocmd ./cmd/*.go + .PHONY: build-release build-release: # i386_linux mkdir -p release/i386_linux cd install && ./prep-install-script.sh ../release/i386_linux && cd .. - cd build && ./build.sh linux 386 ../release/i386_linux ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/i386_linux && cd .. + CGO_ENABLED=0 GOARCH=386 GOOS=linux go build -ldflags=${LDFLAGS} -o release/i386_linux/gocmd cmd/*.go cd release/i386_linux && tar cf gocommands_i386_linux_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/i386_linux # amd64_linux mkdir -p release/amd64_linux cd install && ./prep-install-script.sh ../release/amd64_linux && cd .. - cd build && ./build.sh linux amd64 ../release/amd64_linux ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/amd64_linux && cd .. + CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags=${LDFLAGS} -o release/amd64_linux/gocmd cmd/*.go cd release/amd64_linux && tar cf gocommands_amd64_linux_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/amd64_linux # arm_linux mkdir -p release/arm_linux cd install && ./prep-install-script.sh ../release/arm_linux && cd .. - cd build && ./build.sh linux arm ../release/arm_linux ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/arm_linux && cd .. + CGO_ENABLED=0 GOARCH=arm GOOS=linux go build -ldflags=${LDFLAGS} -o release/arm_linux/gocmd cmd/*.go cd release/arm_linux && tar cf gocommands_arm_linux_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/arm_linux # arm64_linux mkdir -p release/arm64_linux cd install && ./prep-install-script.sh ../release/arm64_linux && cd .. - cd build && ./build.sh linux arm64 ../release/arm64_linux ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/arm64_linux && cd .. + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -ldflags=${LDFLAGS} -o release/arm64_linux/gocmd cmd/*.go cd release/arm64_linux && tar cf gocommands_arm64_linux_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/arm64_linux # amd64_darwin mkdir -p release/amd64_darwin cd install && ./prep-install-script.sh ../release/amd64_darwin && cd .. - cd build && ./build.sh darwin amd64 ../release/amd64_darwin ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/amd64_darwin && cd .. + CGO_ENABLED=0 GOARCH=amd64 GOOS=darwin go build -ldflags=${LDFLAGS} -o release/amd64_darwin/gocmd cmd/*.go cd release/amd64_darwin && tar cf gocommands_amd64_darwin_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/amd64_darwin # arm64_darwin mkdir -p release/arm64_darwin cd install && ./prep-install-script.sh ../release/arm64_darwin && cd .. - cd build && ./build.sh darwin arm64 ../release/arm64_darwin ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/arm64_darwin && cd .. + CGO_ENABLED=0 GOARCH=arm64 GOOS=darwin go build -ldflags=${LDFLAGS} -o release/arm64_darwin/gocmd cmd/*.go cd release/arm64_darwin && tar cf gocommands_arm64_darwin_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/arm64_darwin # i386_windows mkdir -p release/i386_windows cd install && ./prep-install-script.sh ../release/i386_windows && cd .. - cd build && ./build.sh windows 386 ../release/i386_windows ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/i386_windows && cd .. + CGO_ENABLED=0 GOARCH=386 GOOS=windows go build -ldflags=${LDFLAGS} -o release/i386_windows/gocmd cmd/*.go cd release/i386_windows && tar cf gocommands_i386_windows_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/i386_windows # amd64_windows mkdir -p release/amd64_windows cd install && ./prep-install-script.sh ../release/amd64_windows && cd .. - cd build && ./build.sh windows amd64 ../release/amd64_windows ${LDFLAGS} && cd .. + cd install && ./prep-shortcut-script.sh ../release/amd64_windows && cd .. + CGO_ENABLED=0 GOARCH=amd64 GOOS=windows go build -ldflags=${LDFLAGS} -o release/amd64_windows/gocmd cmd/*.go cd release/amd64_windows && tar cf gocommands_amd64_windows_${VERSION}.tar * && mv *.tar .. && cd ../.. rm -rf release/amd64_windows - - diff --git a/build/build.sh b/build/build.sh deleted file mode 100755 index 7be7215..0000000 --- a/build/build.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -main() -{ - local BIN_NAMES=$(ls -1 ../cmd) - local BINS=($BIN_NAMES) - - for i in "${BINS[@]}" - do - echo "building $i for $1 $2" - CGO_ENABLED=0 GOOS=$1 GOARCH=$2 go build -ldflags="$4" -o $3/$i ../cmd/$i - done -} - -set -e -main "$@" diff --git a/cmd/gopwd/gopwd.go b/cmd/gocmd.go similarity index 59% rename from cmd/gopwd/gopwd.go rename to cmd/gocmd.go index 4bdfa43..d2461c7 100644 --- a/cmd/gopwd/gopwd.go +++ b/cmd/gocmd.go @@ -1,9 +1,9 @@ package main import ( - "fmt" "os" + "github.com/cyverse/gocommands/cmd/subcmd" "github.com/cyverse/gocommands/commons" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -11,9 +11,9 @@ import ( // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "gopwd", - Short: "Print current working iRODS collection", - Long: `This prints current working iRODS collection.`, + Use: "gocmd [subcommand]", + Short: "Gocommands, a command-line iRODS client", + Long: `Gocommands, a command-line iRODS client.`, RunE: processCommand, } @@ -36,17 +36,8 @@ func processCommand(command *cobra.Command, args []string) error { return err } - // handle local flags - _, err = commons.InputMissingFields() - if err != nil { - logger.Error(err) - return err - } - - err = printCurrentWorkingDir() - if err != nil { - return err - } + // if nothing is given + commons.PrintHelp(command) return nil } @@ -60,15 +51,22 @@ func main() { // attach common flags commons.SetCommonFlags(rootCmd) + // add sub commands + subcmd.AddInitCommand(rootCmd) + subcmd.AddPwdCommand(rootCmd) + subcmd.AddCdCommand(rootCmd) + subcmd.AddLsCommand(rootCmd) + subcmd.AddCpCommand(rootCmd) + subcmd.AddMvCommand(rootCmd) + subcmd.AddGetCommand(rootCmd) + subcmd.AddPutCommand(rootCmd) + subcmd.AddMkdirCommand(rootCmd) + subcmd.AddRmCommand(rootCmd) + subcmd.AddRmdirCommand(rootCmd) + err := Execute() if err != nil { logger.Fatal(err) os.Exit(1) } } - -func printCurrentWorkingDir() error { - cwd := commons.GetCWD() - fmt.Printf("%s\n", cwd) - return nil -} diff --git a/cmd/gocd/gocd.go b/cmd/subcmd/gocd.go similarity index 70% rename from cmd/gocd/gocd.go rename to cmd/subcmd/gocd.go index b9a0163..f267307 100644 --- a/cmd/gocd/gocd.go +++ b/cmd/subcmd/gocd.go @@ -1,8 +1,7 @@ -package main +package subcmd import ( "fmt" - "os" irodsclient_fs "github.com/cyverse/go-irodsclient/fs" "github.com/cyverse/gocommands/commons" @@ -10,22 +9,24 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "gocd [collection1]", +var cdCmd = &cobra.Command{ + Use: "cd [collection1]", Short: "Change current working iRODS collection", Long: `This changes current working iRODS collection.`, - RunE: processCommand, + RunE: processCdCommand, } -func Execute() error { - return rootCmd.Execute() +func AddCdCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(cdCmd) + + rootCmd.AddCommand(cdCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processCdCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processCdCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -46,8 +47,7 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-cd") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } @@ -66,26 +66,9 @@ func processCommand(command *cobra.Command, args []string) error { return err } } - return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func changeWorkingDir(filesystem *irodsclient_fs.FileSystem, collectionPath string) error { logger := log.WithFields(log.Fields{ "package": "main", diff --git a/cmd/gocp/gocp.go b/cmd/subcmd/gocp.go similarity index 80% rename from cmd/gocp/gocp.go rename to cmd/subcmd/gocp.go index c5a8024..0e5f0f1 100644 --- a/cmd/gocp/gocp.go +++ b/cmd/subcmd/gocp.go @@ -1,8 +1,7 @@ -package main +package subcmd import ( "fmt" - "os" "path/filepath" "strconv" @@ -12,22 +11,26 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "gocp [data-object1] [data-object2] [collection1] ... [target collection]", +var cpCmd = &cobra.Command{ + Use: "cp [data-object1] [data-object2] [collection1] ... [target collection]", Short: "Copy iRODS data-objects or collections to target collection", Long: `This copies iRODS data-objects or collections to the given target collection.`, - RunE: processCommand, + RunE: processCpCommand, } -func Execute() error { - return rootCmd.Execute() +func AddCpCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(cpCmd) + + cpCmd.Flags().BoolP("recurse", "r", false, "Copy recursively") + + rootCmd.AddCommand(cpCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processCpCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processCpCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -57,8 +60,7 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-cp") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } @@ -88,23 +90,6 @@ func processCommand(command *cobra.Command, args []string) error { return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - rootCmd.Flags().BoolP("recurse", "r", false, "Copy recursively") - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func copyOne(filesystem *irodsclient_fs.FileSystem, sourcePath string, targetPath string, recurse bool) error { logger := log.WithFields(log.Fields{ "package": "main", diff --git a/cmd/goget/goget.go b/cmd/subcmd/goget.go similarity index 78% rename from cmd/goget/goget.go rename to cmd/subcmd/goget.go index 94926a5..cd4e4c5 100644 --- a/cmd/goget/goget.go +++ b/cmd/subcmd/goget.go @@ -1,6 +1,7 @@ -package main +package subcmd import ( + "fmt" "os" "path/filepath" @@ -10,22 +11,24 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "goget [data-object1] [data-object2] [collection1] ... [local dir]", +var getCmd = &cobra.Command{ + Use: "get [data-object1] [data-object2] [collection1] ... [local dir]", Short: "Download iRODS data-objects or collections", Long: `This downloads iRODS data-objects or collections to the given local path.`, - RunE: processCommand, + RunE: processGetCommand, } -func Execute() error { - return rootCmd.Execute() +func AddGetCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(getCmd) + + rootCmd.AddCommand(getCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processGetCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processGetCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -46,8 +49,7 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-get") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } @@ -70,27 +72,12 @@ func processCommand(command *cobra.Command, args []string) error { return err } } + } else { + return fmt.Errorf("arguments given are not sufficent") } - return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func getOne(filesystem *irodsclient_fs.FileSystem, sourcePath string, targetPath string) error { cwd := commons.GetCWD() sourcePath = commons.MakeIRODSPath(cwd, sourcePath) @@ -139,6 +126,5 @@ func getDataObject(filesystem *irodsclient_fs.FileSystem, sourcePath string, tar if err != nil { return err } - return nil } diff --git a/cmd/goinit/goinit.go b/cmd/subcmd/goinit.go similarity index 57% rename from cmd/goinit/goinit.go rename to cmd/subcmd/goinit.go index 959dfa9..b565a1a 100644 --- a/cmd/goinit/goinit.go +++ b/cmd/subcmd/goinit.go @@ -1,35 +1,33 @@ -package main +package subcmd import ( "fmt" - "os" - "time" - irodsclient_conn "github.com/cyverse/go-irodsclient/irods/connection" - irodsclient_types "github.com/cyverse/go-irodsclient/irods/types" "github.com/cyverse/gocommands/commons" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "goinit", +var initCmd = &cobra.Command{ + Use: "init", Short: "Initialize gocommands", Long: `This sets up iRODS Host and access account for other gocommands tools. Once the configuration is set, configuration files are created under ~/.irods directory. The configuration is fully-compatible to that of icommands`, - RunE: processCommand, + RunE: processInitCommand, } -func Execute() error { - return rootCmd.Execute() +func AddInitCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(initCmd) + + rootCmd.AddCommand(initCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processInitCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processInitCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -54,7 +52,7 @@ func processCommand(command *cobra.Command, args []string) error { return err } - err = testConnect(account) + err = commons.TestConnect(account) if err != nil { logger.Error(err) return err @@ -75,36 +73,5 @@ func processCommand(command *cobra.Command, args []string) error { return err } } - - return nil -} - -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - -// testConnect just test connection creation -func testConnect(account *irodsclient_types.IRODSAccount) error { - oneMin := 1 * time.Minute - conn := irodsclient_conn.NewIRODSConnection(account, oneMin, "gocommands-init") - - err := conn.Connect() - if err != nil { - return err - } - - defer conn.Disconnect() return nil } diff --git a/cmd/gols/gols.go b/cmd/subcmd/gols.go similarity index 75% rename from cmd/gols/gols.go rename to cmd/subcmd/gols.go index 68d9fe6..6d8aafa 100644 --- a/cmd/gols/gols.go +++ b/cmd/subcmd/gols.go @@ -1,8 +1,7 @@ -package main +package subcmd import ( "fmt" - "os" irodsclient_fs "github.com/cyverse/go-irodsclient/fs" "github.com/cyverse/gocommands/commons" @@ -10,22 +9,24 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "gols [collection1] [collection2] ...", +var lsCmd = &cobra.Command{ + Use: "ls [collection1] [collection2] ...", Short: "List current iRODS collection", Long: `This lists data objects and collections in current iRODS collection.`, - RunE: processCommand, + RunE: processLsCommand, } -func Execute() error { - return rootCmd.Execute() +func AddLsCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(lsCmd) + + rootCmd.AddCommand(lsCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processLsCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processLsCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -46,8 +47,7 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-ls") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } @@ -73,22 +73,6 @@ func processCommand(command *cobra.Command, args []string) error { return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func listColletion(filesystem *irodsclient_fs.FileSystem, collectionPath string) error { logger := log.WithFields(log.Fields{ "package": "main", diff --git a/cmd/gomkdir/gomkdir.go b/cmd/subcmd/gomkdir.go similarity index 67% rename from cmd/gomkdir/gomkdir.go rename to cmd/subcmd/gomkdir.go index b0712f4..e619e06 100644 --- a/cmd/gomkdir/gomkdir.go +++ b/cmd/subcmd/gomkdir.go @@ -1,7 +1,6 @@ -package main +package subcmd import ( - "os" "strconv" irodsclient_fs "github.com/cyverse/go-irodsclient/fs" @@ -10,22 +9,25 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "gomkdir [collection1] [collection2] ...", +var mkdirCmd = &cobra.Command{ + Use: "mkdir [collection1] [collection2] ...", Short: "Make iRODS collections", Long: `This makes iRODS collections.`, - RunE: processCommand, + RunE: processMkdirCommand, } -func Execute() error { - return rootCmd.Execute() +func AddMkdirCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(mkdirCmd) + mkdirCmd.Flags().BoolP("parents", "p", false, "Make parent collections") + + rootCmd.AddCommand(mkdirCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processMkdirCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processMkdirCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -55,8 +57,7 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-mkdir") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } @@ -70,27 +71,9 @@ func processCommand(command *cobra.Command, args []string) error { return err } } - return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - rootCmd.Flags().BoolP("parents", "p", false, "Make parent collections") - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func makeOne(filesystem *irodsclient_fs.FileSystem, targetPath string, parent bool) error { logger := log.WithFields(log.Fields{ "package": "main", diff --git a/cmd/gomv/gomv.go b/cmd/subcmd/gomv.go similarity index 76% rename from cmd/gomv/gomv.go rename to cmd/subcmd/gomv.go index 61c3870..599275e 100644 --- a/cmd/gomv/gomv.go +++ b/cmd/subcmd/gomv.go @@ -1,8 +1,7 @@ -package main +package subcmd import ( "fmt" - "os" irodsclient_fs "github.com/cyverse/go-irodsclient/fs" "github.com/cyverse/gocommands/commons" @@ -10,22 +9,24 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "gomv [data-object1] [data-object2] [collection1] ... [target collection]", +var mvCmd = &cobra.Command{ + Use: "mv [data-object1] [data-object2] [collection1] ... [target collection]", Short: "Move iRODS data-objects or collections to target collection, or rename data-object or collection", Long: `This moves iRODS data-objects or collections to the given target collection, or rename a single data-object or collection.`, - RunE: processCommand, + RunE: processMvCommand, } -func Execute() error { - return rootCmd.Execute() +func AddMvCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(mvCmd) + + rootCmd.AddCommand(mvCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processMvCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processMvCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -46,8 +47,7 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-mv") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } @@ -77,22 +77,6 @@ func processCommand(command *cobra.Command, args []string) error { return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func moveOne(filesystem *irodsclient_fs.FileSystem, sourcePath string, targetPath string) error { logger := log.WithFields(log.Fields{ "package": "main", diff --git a/cmd/goput/goput.go b/cmd/subcmd/goput.go similarity index 78% rename from cmd/goput/goput.go rename to cmd/subcmd/goput.go index 6eba73a..2cb9d6f 100644 --- a/cmd/goput/goput.go +++ b/cmd/subcmd/goput.go @@ -1,6 +1,7 @@ -package main +package subcmd import ( + "fmt" "os" "path/filepath" @@ -10,22 +11,24 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "goput [local file1] [local file2] [local dir1] ... [collection]", +var putCmd = &cobra.Command{ + Use: "put [local file1] [local file2] [local dir1] ... [collection]", Short: "Upload files or directories", Long: `This uploads files or directories to the given iRODS collection.`, - RunE: processCommand, + RunE: processPutCommand, } -func Execute() error { - return rootCmd.Execute() +func AddPutCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(putCmd) + + rootCmd.AddCommand(putCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processPutCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processPutCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -46,8 +49,7 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-put") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } @@ -70,30 +72,15 @@ func processCommand(command *cobra.Command, args []string) error { return err } } + } else { + return fmt.Errorf("arguments given are not sufficent") } - return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func putOne(filesystem *irodsclient_fs.FileSystem, sourcePath string, targetPath string) error { - sourcePath = commons.MakeLocalPath(sourcePath) cwd := commons.GetCWD() + sourcePath = commons.MakeLocalPath(sourcePath) targetPath = commons.MakeIRODSPath(cwd, targetPath) st, err := os.Stat(sourcePath) @@ -139,6 +126,5 @@ func putDataObject(filesystem *irodsclient_fs.FileSystem, sourcePath string, tar if err != nil { return err } - return nil } diff --git a/cmd/subcmd/gopwd.go b/cmd/subcmd/gopwd.go new file mode 100644 index 0000000..b050bc2 --- /dev/null +++ b/cmd/subcmd/gopwd.go @@ -0,0 +1,58 @@ +package subcmd + +import ( + "fmt" + + "github.com/cyverse/gocommands/commons" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var pwdCmd = &cobra.Command{ + Use: "pwd", + Short: "Print current working iRODS collection", + Long: `This prints current working iRODS collection.`, + RunE: processPwdCommand, +} + +func AddPwdCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(pwdCmd) + + rootCmd.AddCommand(pwdCmd) +} + +func processPwdCommand(command *cobra.Command, args []string) error { + logger := log.WithFields(log.Fields{ + "package": "main", + "function": "processPwdCommand", + }) + + cont, err := commons.ProcessCommonFlags(command) + if err != nil { + logger.Error(err) + } + + if !cont { + return err + } + + // handle local flags + _, err = commons.InputMissingFields() + if err != nil { + logger.Error(err) + return err + } + + err = printCurrentWorkingDir() + if err != nil { + return err + } + return nil +} + +func printCurrentWorkingDir() error { + cwd := commons.GetCWD() + fmt.Printf("%s\n", cwd) + return nil +} diff --git a/cmd/gorm/gorm.go b/cmd/subcmd/gorm.go similarity index 73% rename from cmd/gorm/gorm.go rename to cmd/subcmd/gorm.go index 6fe46ef..f6de14e 100644 --- a/cmd/gorm/gorm.go +++ b/cmd/subcmd/gorm.go @@ -1,8 +1,7 @@ -package main +package subcmd import ( "fmt" - "os" "strconv" irodsclient_fs "github.com/cyverse/go-irodsclient/fs" @@ -11,22 +10,26 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "gorm [data-object1] [data-object2] [collection1] ...", +var rmCmd = &cobra.Command{ + Use: "rm [data-object1] [data-object2] [collection1] ...", Short: "Remove iRODS data-objects or collections", Long: `This removes iRODS data-objects or collections.`, - RunE: processCommand, + RunE: processRmCommand, } -func Execute() error { - return rootCmd.Execute() +func AddRmCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(rmCmd) + rmCmd.Flags().BoolP("recurse", "r", false, "Remove non-empty collections") + rmCmd.Flags().BoolP("force", "f", false, "Remove forcefully") + + rootCmd.AddCommand(rmCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processRmCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processRmCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -65,14 +68,17 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-rm") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } defer filesystem.Release() + if len(args) == 0 { + return fmt.Errorf("arguments given are not sufficent") + } + for _, sourcePath := range args { err = removeOne(filesystem, sourcePath, force, recurse) if err != nil { @@ -80,28 +86,9 @@ func processCommand(command *cobra.Command, args []string) error { return err } } - return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - rootCmd.Flags().BoolP("recurse", "r", false, "Remove non-empty collections") - rootCmd.Flags().BoolP("force", "f", false, "Remove forcefully") - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - func removeOne(filesystem *irodsclient_fs.FileSystem, sourcePath string, force bool, recurse bool) error { logger := log.WithFields(log.Fields{ "package": "main", diff --git a/cmd/gormdir/gormdir.go b/cmd/subcmd/gormdir.go similarity index 63% rename from cmd/gormdir/gormdir.go rename to cmd/subcmd/gormdir.go index deee51d..b1fac3c 100644 --- a/cmd/gormdir/gormdir.go +++ b/cmd/subcmd/gormdir.go @@ -1,8 +1,7 @@ -package main +package subcmd import ( "fmt" - "os" irodsclient_fs "github.com/cyverse/go-irodsclient/fs" "github.com/cyverse/gocommands/commons" @@ -10,22 +9,24 @@ import ( "github.com/spf13/cobra" ) -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ - Use: "gormdir [collection1] [collection2] ...", +var rmdirCmd = &cobra.Command{ + Use: "rmdir [collection1] [collection2] ...", Short: "Remove iRODS collections", Long: `This removes iRODS collections.`, - RunE: processCommand, + RunE: processRmdirCommand, } -func Execute() error { - return rootCmd.Execute() +func AddRmdirCommand(rootCmd *cobra.Command) { + // attach common flags + commons.SetCommonFlags(rmdirCmd) + + rootCmd.AddCommand(rmdirCmd) } -func processCommand(command *cobra.Command, args []string) error { +func processRmdirCommand(command *cobra.Command, args []string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "processCommand", + "function": "processRmdirCommand", }) cont, err := commons.ProcessCommonFlags(command) @@ -46,45 +47,31 @@ func processCommand(command *cobra.Command, args []string) error { // Create a file system account := commons.GetAccount() - - filesystem, err := irodsclient_fs.NewFileSystemWithDefault(account, "gocommands-rmdir") + filesystem, err := commons.GetIRODSFSClient(account) if err != nil { return err } defer filesystem.Release() + if len(args) == 0 { + return fmt.Errorf("arguments given are not sufficent") + } + for _, targetPath := range args { - err = removeOne(filesystem, targetPath) + err = removeDirOne(filesystem, targetPath) if err != nil { logger.Error(err) return err } } - return nil } -func main() { - logger := log.WithFields(log.Fields{ - "package": "main", - "function": "main", - }) - - // attach common flags - commons.SetCommonFlags(rootCmd) - - err := Execute() - if err != nil { - logger.Fatal(err) - os.Exit(1) - } -} - -func removeOne(filesystem *irodsclient_fs.FileSystem, targetPath string) error { +func removeDirOne(filesystem *irodsclient_fs.FileSystem, targetPath string) error { logger := log.WithFields(log.Fields{ "package": "main", - "function": "removeOne", + "function": "removeDirOne", }) cwd := commons.GetCWD() diff --git a/commons/commands.go b/commons/commands.go index f5da4cb..4891f2f 100644 --- a/commons/commands.go +++ b/commons/commands.go @@ -111,7 +111,7 @@ func ProcessCommonFlags(command *cobra.Command) (bool, error) { } if help { - printHelp(command) + PrintHelp(command) return false, nil // stop here } } @@ -338,7 +338,7 @@ func printVersion(command *cobra.Command) error { return nil } -func printHelp(command *cobra.Command) error { +func PrintHelp(command *cobra.Command) error { return command.Usage() } diff --git a/commons/irodsclient.go b/commons/irodsclient.go new file mode 100644 index 0000000..0c583ba --- /dev/null +++ b/commons/irodsclient.go @@ -0,0 +1,32 @@ +package commons + +import ( + "time" + + irodsclient_fs "github.com/cyverse/go-irodsclient/fs" + irodsclient_conn "github.com/cyverse/go-irodsclient/irods/connection" + irodsclient_types "github.com/cyverse/go-irodsclient/irods/types" +) + +const ( + ClientProgramName string = "gocommands" +) + +// returns a file system client +func GetIRODSFSClient(account *irodsclient_types.IRODSAccount) (*irodsclient_fs.FileSystem, error) { + return irodsclient_fs.NewFileSystemWithDefault(account, ClientProgramName) +} + +// TestConnect just test connection creation +func TestConnect(account *irodsclient_types.IRODSAccount) error { + oneMin := 1 * time.Minute + conn := irodsclient_conn.NewIRODSConnection(account, oneMin, ClientProgramName) + + err := conn.Connect() + if err != nil { + return err + } + + defer conn.Disconnect() + return nil +} diff --git a/go.mod b/go.mod index 382c89d..38f2fdb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cyverse/gocommands go 1.14 require ( - github.com/cyverse/go-irodsclient v0.9.1-0.20220513225927-484337e3f496 + github.com/cyverse/go-irodsclient v0.9.3 github.com/jedib0t/go-pretty/v6 v6.3.1 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.4.0 diff --git a/go.sum b/go.sum index 9466bb6..d8c9795 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= -github.com/cyverse/go-irodsclient v0.9.1-0.20220513225927-484337e3f496 h1:h4i2s1z3+61LaVW2df2Uq+PGNrndvH5MlIDYTfQ8zx8= -github.com/cyverse/go-irodsclient v0.9.1-0.20220513225927-484337e3f496/go.mod h1:vEllPYPlJhNUp/op6e4/zDKt/wOIeN8Y7Nyy4QpR/F0= +github.com/cyverse/go-irodsclient v0.9.3 h1:ERfEZJYOH8ZvAceKD68iCfesWyMyqGf/VBVUwxudD9Y= +github.com/cyverse/go-irodsclient v0.9.3/go.mod h1:vEllPYPlJhNUp/op6e4/zDKt/wOIeN8Y7Nyy4QpR/F0= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/install/install.sh.template b/install/install.sh.template index 80d2c98..7a445e7 100644 --- a/install/install.sh.template +++ b/install/install.sh.template @@ -1,8 +1,10 @@ #! /bin/bash -BINS=($BIN_NAMES) +SUBCOMMANDS=($SUBCOMMAND_NAMES) -for i in "${BINS[@]}" +cp ./gocmd /usr/bin + +for i in "${SUBCOMMANDS[@]}" do - cp ./$i /usr/bin + cp ./go$i /usr/bin done \ No newline at end of file diff --git a/install/prep-install-script.sh b/install/prep-install-script.sh index 4696b5d..7456c8b 100755 --- a/install/prep-install-script.sh +++ b/install/prep-install-script.sh @@ -3,8 +3,10 @@ main() { mkdir -p $1 + expand_tmpl install.sh.template > $1/install.sh chmod 700 $1/install.sh + expand_tmpl uninstall.sh.template > $1/uninstall.sh chmod 700 $1/uninstall.sh } @@ -24,10 +26,10 @@ escape() expand_tmpl() { - local BIN_NAMES=$(ls -1 ../cmd) + local SUBCOMMAND_NAMES=$(ls -1 ../cmd/subcmd | sed -e 's/\.go$//' | sed -e 's/^go//') cat < $1/go$i + chmod 700 $1/go$i + done +} + +set -e +main "$@" diff --git a/install/uninstall.sh.template b/install/uninstall.sh.template index 8dcc9c4..4e517bd 100644 --- a/install/uninstall.sh.template +++ b/install/uninstall.sh.template @@ -1,8 +1,10 @@ #! /bin/bash -BINS=($BIN_NAMES) +SUBCOMMANDS=($SUBCOMMAND_NAMES) -for i in "${BINS[@]}" +rm /usr/bin/gocmd + +for i in "${SUBCOMMANDS[@]}" do - rm /usr/bin/$i + rm /usr/bin/go$i done \ No newline at end of file