Skip to content

Fix/break challenge on creatdir #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func PrintError(v ...interface{}) {
}

func PrintInfo(v ...interface{}) {
fmt.Fprintln(os.Stdin, v...)
fmt.Fprintln(os.Stdout, v...)
}

func PrintInfof(format string, v ...interface{}) {
fmt.Fprintln(os.Stdout, fmt.Sprintf(format, v...))
}

func commitMetaTxn(path, crudOp, authTicket, lookupHash string, a *sdk.Allocation, fileMeta *sdk.ConsolidatedFileMeta, status *StatusBar) {
Expand Down
76 changes: 76 additions & 0 deletions cmd/dir_new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"context"
"os"

"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)

var (
dirNewAllocationID string
dirNewName string
)

func init() {
rootCmd.AddCommand(createDirCmd)
createDirCmd.Flags().String("allocation", "", "Allocation ID")
createDirCmd.Flags().String("dirname", "", "New directory name")
createDirCmd.MarkFlagRequired("allocation")
createDirCmd.MarkFlagRequired("dirname")

rootCmd.AddCommand(dirNewCmd)
dirNewCmd.Flags().StringVarP(&dirNewAllocationID, "alloc", "a", "", "allocation id")
dirNewCmd.Flags().StringVarP(&dirNewName, "name", "n", "", "directory name")
dirNewCmd.MarkFlagRequired("alloc") //nolint
dirNewCmd.MarkFlagRequired("name") //nolint
}

// use dir-create instead
var createDirCmd = &cobra.Command{
Use: "createdir",
Deprecated: "please use mkdir",
Short: "Create directory in allocation",
Long: `Create directory in allocation`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if !fflags.Changed("allocation") { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and return
}
if !fflags.Changed("dirname") {
PrintError("Error: dirname flag is missing")
os.Exit(1)
}

allocationID := cmd.Flag("allocation").Value.String()
dirname := cmd.Flag("dirname").Value.String()

err := sdk.CreateDir(context.TODO(), allocationID, dirname)

if err != nil {
PrintError("ERR: ", err.Error())
return
}

PrintInfof("OK: created directory '%s'", dirNewName)
},
}

var dirNewCmd = &cobra.Command{
Use: "dir-new",
Short: "Create directories named on blobbers",
Long: `Create directories named on blobbers`,
Run: func(cmd *cobra.Command, args []string) {
err := sdk.CreateDir(context.TODO(), dirNewAllocationID, dirNewName)

if err != nil {
PrintError("ERR: ", err.Error())
return
}

PrintInfof("OK: created directory '%s'", dirNewName)
},
}
46 changes: 1 addition & 45 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,6 @@ import (
"github.com/spf13/cobra"
)

var createDirCmd = &cobra.Command{
Use: "createdir",
Short: "Create directory",
Long: `Create directory`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if !fflags.Changed("allocation") { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and return
}
if !fflags.Changed("dirname") {
PrintError("Error: dirname flag is missing")
os.Exit(1)
}

allocationID := cmd.Flag("allocation").Value.String()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation.", err)
os.Exit(1)
}
dirname := cmd.Flag("dirname").Value.String()

if err != nil {
PrintError("CreateDir failed.", err)
os.Exit(1)
}
err = allocationObj.CreateDir(dirname)

if err != nil {
PrintError("CreateDir failed.", err)
os.Exit(1)
}

return
},
}

// uploadCmd represents upload command
var uploadCmd = &cobra.Command{
Use: "upload",
Expand Down Expand Up @@ -299,7 +260,7 @@ func startSyncUpload(cmd *cobra.Command, allocationObj *sdk.Allocation, localPat

func init() {
rootCmd.AddCommand(uploadCmd)
rootCmd.AddCommand(createDirCmd)

uploadCmd.PersistentFlags().String("allocation", "", "Allocation ID")
uploadCmd.PersistentFlags().String("remotepath", "", "Remote path to upload")
uploadCmd.PersistentFlags().String("localpath", "", "Local path of file to upload")
Expand All @@ -325,9 +286,4 @@ func init() {
uploadCmd.MarkFlagRequired("remotepath")
uploadCmd.MarkFlagRequired("localpath")

createDirCmd.PersistentFlags().String("allocation", "", "Allocation ID")
createDirCmd.PersistentFlags().String("dirname", "", "New directory name")
createDirCmd.MarkFlagRequired("allocation")
createDirCmd.MarkFlagRequired("dirname")

}
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.13

require (
github.com/0chain/errors v1.0.2
github.com/0chain/gosdk v1.3.0-beta.1
github.com/0chain/gosdk v1.3.0-beta.1.0.20211019015113-9364f2b49e6c
github.com/mattn/go-runewidth v0.0.10 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/0chain/errors v1.0.2 h1:IIUMeh/qFlqDcyHesjU92CpRMVz9dIQWAtZooqrYinA=
github.com/0chain/errors v1.0.2/go.mod h1:5t76jLb56TKfg/K2VD+eUMmNZJ42QsIRI8KzWuztwU4=
github.com/0chain/gosdk v1.3.0-beta.1 h1:0Jt7BWapTi7zns+UH35YJF/XOgiaDqoF9zYLCeCsCds=
github.com/0chain/gosdk v1.3.0-beta.1/go.mod h1:JtvcqYYWRdOVFm0pvjdKO5pCiItc/Is2f5wTuuA8F4M=
github.com/0chain/gosdk v1.3.0-beta.1.0.20211019015113-9364f2b49e6c h1:TlKWJ6cVnwGKwF31CZdh4wX7IoiEGsezcBv7M5sFXvA=
github.com/0chain/gosdk v1.3.0-beta.1.0.20211019015113-9364f2b49e6c/go.mod h1:JtvcqYYWRdOVFm0pvjdKO5pCiItc/Is2f5wTuuA8F4M=
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc=
github.com/Azure/azure-storage-blob-go v0.7.0/go.mod h1:f9YQKtsG1nMisotuTPpO0tjNuEjKRYAcJU8/ydDI++4=
Expand Down