Skip to content

Commit

Permalink
feat(fsi): Init can take a directory name to create for linking
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Sep 6, 2019
1 parent f49b34d commit d62b816
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
41 changes: 41 additions & 0 deletions cmd/fsi_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,47 @@ run ` + "`qri save`" + ` to commit this dataset
}
}

// Test init with a directory will create that directory
func TestInitWithDirectory(t *testing.T) {
fr := NewFSITestRunner(t, "qri_test_init_with_directory")
defer fr.Delete()

fr.ChdirToRoot()

// Init with a directory to create.
if err := fr.ExecCommand(fmt.Sprintf("qri init init_dir --name init_dir --format csv")); err != nil {
t.Fatalf(err.Error())
}

// Directory has been created by `qri init`
workDir := fr.ChdirToWorkDir("init_dir")

// Verify the directory contains the files that we expect.
dirContents := listDirectory(workDir)
expectContents := []string{".qri-ref", "body.csv", "meta.json", "schema.json"}
if diff := cmp.Diff(dirContents, expectContents); diff != "" {
t.Errorf("directory contents (-want +got):\n%s", diff)
}

// Status, check that the working directory has added files.
if err := fr.ExecCommand("qri status"); err != nil {
t.Fatalf(err.Error())
}

output := fr.GetCommandOutput()
expect := `for linked dataset [test_peer/init_dir]
add: meta (source: meta.json)
add: schema (source: schema.json)
add: body (source: body.csv)
run ` + "`qri save`" + ` to commit this dataset
`
if diff := cmpTextLines(expect, output); diff != "" {
t.Errorf("qri status (-want +got):\n%s", diff)
}
}

func parseRefFromSave(output string) string {
pos := strings.Index(output, "saved: ")
ref := output[pos+7:]
Expand Down
20 changes: 16 additions & 4 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func NewInitCommand(f Factory, ioStreams ioes.IOStreams) *cobra.Command {
"group": "dataset",
},
RunE: func(cmd *cobra.Command, args []string) error {
o.Complete(f)
if err := o.Complete(f, args); err != nil {
return err
}
return o.Run()
},
}
Expand All @@ -42,17 +44,21 @@ type InitOptions struct {
Name string
Format string
SourceBodyPath string
Mkdir string

DatasetRequests *lib.DatasetRequests
FSIMethods *lib.FSIMethods
}

// Complete completes a dataset reference
func (o *InitOptions) Complete(f Factory) (err error) {
func (o *InitOptions) Complete(f Factory, args []string) (err error) {
if o.DatasetRequests, err = f.DatasetRequests(); err != nil {
return err
}
o.FSIMethods, err = f.FSIMethods()
if len(args) > 0 && args[0] != "." {
o.Mkdir = args[0]
}
return err
}

Expand All @@ -63,9 +69,14 @@ func (o *InitOptions) Run() (err error) {
return err
}

// Suggestion for the dataset name defaults to the name of the current directory.
// Suggestion for the dataset name defaults to directory it is being linked into
if o.Name == "" {
suggestedName := varName.CreateVarNameFromString(filepath.Base(pwd))
var suggestedName string
if o.Mkdir == "" {
suggestedName = varName.CreateVarNameFromString(filepath.Base(pwd))
} else {
suggestedName = varName.CreateVarNameFromString(o.Mkdir)
}
o.Name = inputText(o.ErrOut, o.In, "Name of new dataset", suggestedName)
}

Expand All @@ -75,6 +86,7 @@ func (o *InitOptions) Run() (err error) {

p := &lib.InitFSIDatasetParams{
Dir: pwd,
Mkdir: o.Mkdir,
Format: o.Format,
Name: o.Name,
SourceBodyPath: o.SourceBodyPath,
Expand Down
26 changes: 21 additions & 5 deletions fsi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type InitParams struct {
Dir string
Name string
Format string
Mkdir string
SourceBodyPath string
}

Expand All @@ -30,7 +31,22 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) {
return "", fmt.Errorf("invalid path to initialize. '%s' is not a directory", p.Dir)
}

if err = canInitDir(p.Dir); err != nil {
// TODO(dlong): This function should more closely resemble Checkout in lib/fsi.go

// Either use an existing directory, or create one at the given directory.
var targetPath string
if p.Mkdir == "" {
targetPath = p.Dir
} else {
targetPath = filepath.Join(p.Dir, p.Mkdir)
// Create the directory. It is not an error for the directory to already exist, as long
// as it is not already linked, which is checked below.
if err := os.Mkdir(targetPath, os.ModePerm); err != nil {
return "", err
}
}

if err = canInitDir(targetPath); err != nil {
return "", err
}

Expand All @@ -57,7 +73,7 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) {
}

// Create the link file, containing the dataset reference.
if name, err = fsi.CreateLink(p.Dir, ref.AliasString()); err != nil {
if name, err = fsi.CreateLink(targetPath, ref.AliasString()); err != nil {
return name, err
}

Expand All @@ -69,7 +85,7 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) {
"homeURL": ""
}
`)
if err := ioutil.WriteFile(filepath.Join(p.Dir, "meta.json"), metaSkeleton, os.ModePerm); err != nil {
if err := ioutil.WriteFile(filepath.Join(targetPath, "meta.json"), metaSkeleton, os.ModePerm); err != nil {
return name, err
}

Expand Down Expand Up @@ -107,7 +123,7 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) {
}
}
data, err = json.MarshalIndent(schema, "", " ")
if err := ioutil.WriteFile(filepath.Join(p.Dir, "schema.json"), data, os.ModePerm); err != nil {
if err := ioutil.WriteFile(filepath.Join(targetPath, "schema.json"), data, os.ModePerm); err != nil {
return name, err
}

Expand All @@ -129,7 +145,7 @@ func (fsi *FSI) InitDataset(p InitParams) (name string, err error) {
return "", err
}
}
bodyFilename := filepath.Join(p.Dir, fmt.Sprintf("body.%s", p.Format))
bodyFilename := filepath.Join(targetPath, fmt.Sprintf("body.%s", p.Format))
if err := ioutil.WriteFile(bodyFilename, bodyBytes, os.ModePerm); err != nil {
return "", err
}
Expand Down

0 comments on commit d62b816

Please sign in to comment.