Skip to content

Commit

Permalink
Merge pull request #50 from cyverse/add_touch
Browse files Browse the repository at this point in the history
Add touch
  • Loading branch information
iychoi authored Aug 27, 2024
2 parents fc278c9 + d975371 commit fd75fef
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 26 deletions.
21 changes: 21 additions & 0 deletions cmd/flag/touch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package flag

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

type NoCreateFlagValues struct {
NoCreate bool
}

var (
noCreateFlagValues NoCreateFlagValues
)

func SetNoCreateFlags(command *cobra.Command) {
command.Flags().BoolVar(&noCreateFlagValues.NoCreate, "no_create", false, "Do not create data object")
}

func GetNoCreateFlagValues() *NoCreateFlagValues {
return &noCreateFlagValues
}
12 changes: 12 additions & 0 deletions cmd/gocmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
subcmd.AddPwdCommand(rootCmd)
subcmd.AddCdCommand(rootCmd)
subcmd.AddLsCommand(rootCmd)
subcmd.AddTouchCommand(rootCmd)
subcmd.AddCpCommand(rootCmd)
subcmd.AddMvCommand(rootCmd)
subcmd.AddCatCommand(rootCmd)
Expand Down Expand Up @@ -107,6 +108,10 @@ func main() {
if err != nil {
logger.Errorf("%+v", err)

if flag.GetCommonFlagValues(rootCmd).DebugMode {
commons.PrintErrorf("%+v\n", err)
}

if os.IsNotExist(err) {
commons.PrintErrorf("File or directory not found!\n")
} else if irodsclient_types.IsConnectionConfigError(err) {
Expand Down Expand Up @@ -181,6 +186,13 @@ func main() {
} else {
commons.PrintErrorf("Destination is not a directory!\n")
}
} else if commons.IsNotFileError(err) {
var notFileError *commons.NotFileError
if errors.As(err, &notFileError) {
commons.PrintErrorf("Destination %q is not a file!\n", notFileError.Path)
} else {
commons.PrintErrorf("Destination is not a file!\n")
}
} else {
commons.PrintErrorf("Unexpected error!\nError Trace:\n - %+v\n", err)
}
Expand Down
9 changes: 1 addition & 8 deletions cmd/subcmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,7 @@ func (ls *LsCommand) listOne(sourcePath string) error {
}

// data object
parentSourcePath := path.Dir(sourcePath)

parentCollection, err := irodsclient_irodsfs.GetCollection(connection, parentSourcePath)
if err != nil {
return xerrors.Errorf("failed to get collection %q: %w", parentSourcePath, err)
}

entry, err := irodsclient_irodsfs.GetDataObject(connection, parentCollection, path.Base(sourcePath))
entry, err := irodsclient_irodsfs.GetDataObjectWithoutCollection(connection, sourcePath)
if err != nil {
return xerrors.Errorf("failed to get data-object %q: %w", sourcePath, err)
}
Expand Down
136 changes: 136 additions & 0 deletions cmd/subcmd/touch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package subcmd

import (
irodsclient_fs "github.com/cyverse/go-irodsclient/fs"
irodsclient_types "github.com/cyverse/go-irodsclient/irods/types"
"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 touchCmd = &cobra.Command{
Use: "touch [data-object]",
Aliases: []string{"itouch"},
Short: "Create an empty iRODS data-object or update last modified time of existing data-object",
Long: `This displays the content of an iRODS data-object.`,
RunE: processTouchCommand,
Args: cobra.MinimumNArgs(1),
}

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

flag.SetTicketAccessFlags(touchCmd)
flag.SetNoCreateFlags(touchCmd)

rootCmd.AddCommand(touchCmd)
}

func processTouchCommand(command *cobra.Command, args []string) error {
touch, err := NewTouchCommand(command, args)
if err != nil {
return err
}

return touch.Process()
}

type TouchCommand struct {
command *cobra.Command

ticketAccessFlagValues *flag.TicketAccessFlagValues
noCreateFlagValues *flag.NoCreateFlagValues

account *irodsclient_types.IRODSAccount
filesystem *irodsclient_fs.FileSystem

targetPaths []string
}

func NewTouchCommand(command *cobra.Command, args []string) (*TouchCommand, error) {
touch := &TouchCommand{
command: command,

ticketAccessFlagValues: flag.GetTicketAccessFlagValues(),
noCreateFlagValues: flag.GetNoCreateFlagValues(),
}

// path
touch.targetPaths = args

return touch, nil
}

func (touch *TouchCommand) Process() error {
logger := log.WithFields(log.Fields{
"package": "subcmd",
"struct": "TouchCommand",
"function": "Process",
})

cont, err := flag.ProcessCommonFlags(touch.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)
}

// config
appConfig := commons.GetConfig()
syncAccount := false
if len(touch.ticketAccessFlagValues.Name) > 0 {
logger.Debugf("use ticket: %q", touch.ticketAccessFlagValues.Name)
appConfig.Ticket = touch.ticketAccessFlagValues.Name
syncAccount = true
}

if syncAccount {
err := commons.SyncAccount()
if err != nil {
return err
}
}

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

// run
for _, targetPath := range touch.targetPaths {
err = touch.touchOne(targetPath)
if err != nil {
return xerrors.Errorf("failed to touch %q: %w", targetPath, err)
}
}

return nil
}

func (touch *TouchCommand) touchOne(targetPath string) error {
cwd := commons.GetCWD()
home := commons.GetHomeDir()
zone := commons.GetZone()
targetPath = commons.MakeIRODSPath(cwd, home, zone, targetPath)

err := touch.filesystem.Touch(targetPath, "", touch.noCreateFlagValues.NoCreate)
if err != nil {
return xerrors.Errorf("failed to touch file %q: %w", targetPath, err)
}

return nil
}
13 changes: 9 additions & 4 deletions commons/constant.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package commons

import "time"

const (
TransferTreadNumDefault int = 5
UploadTreadNumMax int = 20
TcpBufferSizeDefault int = 4 * 1024 * 1024
TcpBufferSizeStringDefault string = "4MB"
clientProgramName string = "md-repo-cli"
connectionTimeout time.Duration = 10 * time.Minute
filesystemTimeout time.Duration = 10 * time.Minute
TransferTreadNumDefault int = 5
UploadTreadNumMax int = 20
TcpBufferSizeDefault int = 4 * 1024 * 1024
TcpBufferSizeStringDefault string = "4MB"

RedirectToResourceMinSize int64 = 1024 * 1024 * 1024 // 1GB
ParallelUploadMinSize int64 = 80 * 1024 * 1024 // 80MB
Expand Down
25 changes: 14 additions & 11 deletions commons/irods_client.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
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"
"golang.org/x/xerrors"
)

const (
ClientProgramName string = "gocommands"
connectionTimeout time.Duration = 10 * time.Minute
filesystemTimeout time.Duration = 10 * time.Minute
)

// GetIRODSFSClient returns a file system client
func GetIRODSFSClient(account *irodsclient_types.IRODSAccount) (*irodsclient_fs.FileSystem, error) {
fsConfig := irodsclient_fs.NewFileSystemConfig(ClientProgramName, irodsclient_fs.FileSystemConnectionErrorTimeoutDefault, irodsclient_fs.FileSystemConnectionInitNumberDefault, irodsclient_fs.FileSystemConnectionLifespanDefault,
fsConfig := irodsclient_fs.NewFileSystemConfig(clientProgramName, irodsclient_fs.FileSystemConnectionErrorTimeoutDefault, irodsclient_fs.FileSystemConnectionInitNumberDefault, irodsclient_fs.FileSystemConnectionLifespanDefault,
filesystemTimeout, filesystemTimeout, irodsclient_fs.FileSystemConnectionMaxDefault, TcpBufferSizeDefault,
irodsclient_fs.FileSystemTimeoutDefault, irodsclient_fs.FileSystemTimeoutDefault, []irodsclient_fs.MetadataCacheTimeoutSetting{}, true, true)

Expand All @@ -34,7 +26,7 @@ func GetIRODSFSClientAdvanced(account *irodsclient_types.IRODSAccount, maxConnec
tcpBufferSize = TcpBufferSizeDefault
}

fsConfig := irodsclient_fs.NewFileSystemConfig(ClientProgramName, irodsclient_fs.FileSystemConnectionErrorTimeoutDefault, irodsclient_fs.FileSystemConnectionInitNumberDefault, irodsclient_fs.FileSystemConnectionLifespanDefault,
fsConfig := irodsclient_fs.NewFileSystemConfig(clientProgramName, irodsclient_fs.FileSystemConnectionErrorTimeoutDefault, irodsclient_fs.FileSystemConnectionInitNumberDefault, irodsclient_fs.FileSystemConnectionLifespanDefault,
filesystemTimeout, filesystemTimeout, maxConnection, tcpBufferSize,
irodsclient_fs.FileSystemTimeoutDefault, irodsclient_fs.FileSystemTimeoutDefault, []irodsclient_fs.MetadataCacheTimeoutSetting{}, true, true)

Expand All @@ -43,11 +35,22 @@ func GetIRODSFSClientAdvanced(account *irodsclient_types.IRODSAccount, maxConnec

// GetIRODSConnection returns a connection
func GetIRODSConnection(account *irodsclient_types.IRODSAccount) (*irodsclient_conn.IRODSConnection, error) {
conn := irodsclient_conn.NewIRODSConnection(account, connectionTimeout, ClientProgramName)
conn := irodsclient_conn.NewIRODSConnection(account, connectionTimeout, clientProgramName)
err := conn.Connect()
if err != nil {
return nil, xerrors.Errorf("failed to connect: %w", err)
}

return conn, nil
}

// TestConnect just test connection creation
func TestConnect(account *irodsclient_types.IRODSAccount) error {
conn, err := GetIRODSConnection(account)
if err != nil {
return nil
}

defer conn.Disconnect()
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/creativeprojects/go-selfupdate v1.0.1
github.com/cyverse/go-irodsclient v0.14.14
github.com/cyverse/go-irodsclient v0.14.16-0.20240827195018-78c6ec27b7e4
github.com/dustin/go-humanize v1.0.1
github.com/gliderlabs/ssh v0.3.5
github.com/jedib0t/go-pretty/v6 v6.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creativeprojects/go-selfupdate v1.0.1 h1:5Un4MTv4puCR5GBgkDLC14J72fljGuMC60E63cFGq1o=
github.com/creativeprojects/go-selfupdate v1.0.1/go.mod h1:nm7AWUJfrfYt/SB97NAcMhR0KEpPqlrVHXkWFti+ezw=
github.com/cyverse/go-irodsclient v0.14.14 h1:Q9Bl8lu3SIUjGlAIPyGmj71QBQM8KmXwnYmFFa7xsl4=
github.com/cyverse/go-irodsclient v0.14.14/go.mod h1:eBXha3cwfrM0p1ijYVqsrLJQHpRwTfpA4c5dKCQsQFc=
github.com/cyverse/go-irodsclient v0.14.16-0.20240827195018-78c6ec27b7e4 h1:swtNISQo69vhNZp/GhvPhFMR8IccFGaW1+8ja3cimy8=
github.com/cyverse/go-irodsclient v0.14.16-0.20240827195018-78c6ec27b7e4/go.mod h1:eBXha3cwfrM0p1ijYVqsrLJQHpRwTfpA4c5dKCQsQFc=
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=
Expand Down

0 comments on commit fd75fef

Please sign in to comment.