Skip to content

Commit

Permalink
refactor(cmd): initial refactor of commands package
Browse files Browse the repository at this point in the history
removed templated license headers, general package cleanup
  • Loading branch information
b5 committed Nov 9, 2017
1 parent 7bacc02 commit 60bce71
Show file tree
Hide file tree
Showing 22 changed files with 189 additions and 511 deletions.
15 changes: 15 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"path/filepath"
"runtime"

"github.com/spf13/viper"
Expand Down Expand Up @@ -49,3 +50,17 @@ func userHomeDir() string {
}
return os.Getenv("HOME")
}

func loadFileIfPath(path string, f *os.File) (err error) {
if path == "" {
return nil
}

filepath, err := filepath.Abs(path)
if err != nil {
return err
}

f, err = os.Open(filepath)
return
}
17 changes: 1 addition & 16 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand All @@ -24,8 +10,7 @@ import (
)

type Config struct {
// Remotes []*Remote `json:"remotes"`
// Folders []*Folder `json:"folders"`
Bootstrap []string
}

// configCmd represents the config command
Expand Down
25 changes: 4 additions & 21 deletions cmd/dataset.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down Expand Up @@ -54,8 +40,7 @@ var datasetInfoCmd = &cobra.Command{
if len(args) != 1 {
ErrExit(fmt.Errorf("wrong number of arguments. expected qri info [dataset_name]"))
}
ds, err := GetIpfsFilestore(true)
ExitIfErr(err)
ds := GetIpfsFilestore(true)

path, err := GetRepo(true).GetPath(args[0])
ExitIfErr(err)
Expand Down Expand Up @@ -83,8 +68,7 @@ var datasetAddCmd = &cobra.Command{
}

r := GetRepo(false)
fs, err := GetIpfsFilestore(false)
ExitIfErr(err)
fs := GetIpfsFilestore(false)

name := cmd.Flag("name").Value.String()
if name == "" {
Expand All @@ -94,7 +78,7 @@ var datasetAddCmd = &cobra.Command{
root := strings.TrimSuffix(args[0], "/"+dsfs.PackageFileDataset.String())

PrintInfo("downloading %s...", root)
_, err = fs.Fetch(cafs.SourceAny, datastore.NewKey(root))
_, err := fs.Fetch(cafs.SourceAny, datastore.NewKey(root))
ExitIfErr(err)

err = fs.Pin(datastore.NewKey(root), true)
Expand All @@ -118,8 +102,7 @@ var datasetRemoveCmd = &cobra.Command{
}
name := args[0]

fs, err := GetIpfsFilestore(false)
ExitIfErr(err)
fs := GetIpfsFilestore(false)

r := GetRepo(false)
path, err := r.GetPath(name)
Expand Down
37 changes: 18 additions & 19 deletions cmd/export.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"github.com/qri-io/dataset/dsfs"
"github.com/qri-io/dataset/dsutil"
"github.com/ipfs/go-datastore"
"io"
"os"

"github.com/qri-io/dataset/dsfs"
"github.com/qri-io/dataset/dsutil"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
)

Expand All @@ -41,12 +30,22 @@ var exportCmd = &cobra.Command{
}

r := GetRepo(false)
store, err := GetIpfsFilestore(false)
ExitIfErr(err)
store := GetIpfsFilestore(false)

req := core.NewDatasetRequests(store, r)

ds, err := FindDataset(r, store, args[0])
p := &core.GetDatasetParams{
Name: args[0],
Path: datastore.NewKey(args[0]),
}
res := &repo.DatasetRef{}
err := req.Get(p, res)
ExitIfErr(err)

ds := res.Dataset

// ds, err := FindDataset(r, store, args[0])

if cmd.Flag("data-only").Value.String() == "true" {
src, err := dsfs.LoadData(store, ds)
ExitIfErr(err)
Expand Down
14 changes: 0 additions & 14 deletions cmd/info.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down
14 changes: 0 additions & 14 deletions cmd/init-ipfs.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down
41 changes: 8 additions & 33 deletions cmd/init.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down Expand Up @@ -43,29 +29,17 @@ var initCmd = &cobra.Command{

if initFile == "" && initUrl == "" {
ErrExit(fmt.Errorf("please provide either a file or a url argument"))
}

if initName == "" {
} else if initName == "" {
ErrExit(fmt.Errorf("please provide a --name"))
}

if initFile != "" {
filepath, err := filepath.Abs(initFile)
ExitIfErr(err)
dataFile, err = os.Open(filepath)
ExitIfErr(err)
}

if initMetaFile != "" {
filepath, err := filepath.Abs(initMetaFile)
ExitIfErr(err)
metaFile, err = os.Open(filepath)
ExitIfErr(err)
}
err := loadFileIfPath(initFile, dataFile)
ExitIfErr(err)
err = loadFileIfPath(initMetaFile, metaFile)
ExitIfErr(err)

r := GetRepo(false)
store, err := GetIpfsFilestore(false)
ExitIfErr(err)
store := GetIpfsFilestore(false)
req := core.NewDatasetRequests(store, r)

p := &core.InitDatasetParams{
Expand All @@ -74,7 +48,8 @@ var initCmd = &cobra.Command{
DataFilename: filepath.Base(initFile),
}

// this is because passing nil to interfaces is bad: https://golang.org/doc/faq#nil_error
// this is because passing nil to interfaces is bad
// see: https://golang.org/doc/faq#nil_error
if dataFile != nil {
p.Data = dataFile
}
Expand Down
22 changes: 0 additions & 22 deletions cmd/input.go

This file was deleted.

13 changes: 0 additions & 13 deletions cmd/ipfs.go

This file was deleted.

14 changes: 0 additions & 14 deletions cmd/peer.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down
17 changes: 17 additions & 0 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"github.com/qri-io/qri/repo"
"os"
"strings"
"time"

sp "github.com/briandowns/spinner"
Expand Down Expand Up @@ -158,3 +159,19 @@ func PrintResults(r *dataset.Structure, data []byte, format dataset.DataFormat)
// }
// }
// }

func prompt(msg string) string {
var input string
printPrompt(msg)
fmt.Scanln(&input)
return strings.TrimSpace(input)
}

func InputText(message, defaultText string) string {
if message == "" {
message = "enter text:"
}
input := prompt(fmt.Sprintf("%s [%s]: ", message, defaultText))

return input
}
14 changes: 0 additions & 14 deletions cmd/queries.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
// Copyright © 2016 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
Expand Down
Loading

0 comments on commit 60bce71

Please sign in to comment.