cmd

package
v0.0.0-...-354d71b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2019 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExportCmd = &cobra.Command{
	Use:   "export",
	Short: "write a qri dataset as a datapackage zip archive",
	Example: `$ qri-datapackage export me/dataset

$ qri-datapackage export me/dataset package.zip`,
	Args: cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		name := args[0]
		stream := &bytes.Buffer{}
		qriCmd := exec.Command("qri", "export", "--format=json", "--output=temp.json", name)
		qriCmd.Stdout = stream
		if err := qriCmd.Run(); err != nil {
			fmt.Println(stream.String())
			fmt.Printf("qri export error: %s\n", err.Error())
			return
		}
		defer os.Remove("temp.json")
		data, err := ioutil.ReadFile("temp.json")
		if err != nil {
			fmt.Printf("error opening data: %s", err.Error())
			return
		}

		ds := &dataset.Dataset{}
		if err := ds.UnmarshalJSON(data); err != nil {
			fmt.Printf("error unmarshaling dataset: %s", err.Error())
			return
		}

		pkg, err := DatasetToDataPackage(ds)
		if err != nil {
			fmt.Printf("error creating datapackage: %s", err.Error())
			return
		}

		pkgName := ds.Name + "_datapackage.zip"
		if len(args) == 2 {
			pkgName = args[1]
		}
		if err = pkg.Zip(pkgName); err != nil {
			fmt.Printf("error writing zip: %s", err.Error())
			return
		}
		fmt.Printf("exported datapackage zip archive to: %s\n", pkgName)
	},
}

ExportCmd turns a Qri datasets into an open knowledge foundation datapackage

View Source
var ImportCmd = &cobra.Command{
	Use:   "import",
	Short: "import a datapackage into qri",
	Args:  cobra.ExactArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		pkgPath := args[0]
		pkg, err := datapackage.Load(pkgPath)
		if err != nil {
			fmt.Printf("error loading data package: %s\n", err.Error())
			return
		}
		log.Debugf("Data package '%s' loaded.\n", pkg.Descriptor()["name"])

		ds, err := dataPackageToDataset(pkg)
		if err != nil {
			fmt.Printf("error converting to dataset: %s\n", err.Error())
			return
		}

		body := ds.Body
		ds.Body = nil

		dsPath := filepath.Join(filepath.Dir(pkgPath), "dataset.json")
		f, err := os.Create(dsPath)
		if err != nil {
			fmt.Printf("error creating temp dataset file: %s\n", err.Error())
			return
		}
		if err := json.NewEncoder(f).Encode(ds); err != nil {
			fmt.Printf("error encoding dataset: %s\n", err.Error())
			return
		}
		f.Close()
		defer os.Remove(dsPath)

		bodyPath := filepath.Join(filepath.Dir(pkgPath), "body.json")
		bodyF, err := os.Create(bodyPath)
		if err != nil {
			fmt.Printf("error creating temp dataset file: %s\n", err.Error())
			return
		}
		if err := json.NewEncoder(bodyF).Encode(body); err != nil {
			fmt.Printf("error encoding dataset: %s\n", err.Error())
			return
		}
		bodyF.Close()
		defer os.Remove(bodyPath)

		name := filepath.Base(filepath.Dir(pkgPath))
		if str, ok := pkg.Descriptor()["name"].(string); ok {
			name = str
		}
		log.Debugf("using name: %s", name)

		qriCmd := exec.Command("qri", "save", "--file="+dsPath, "--body="+bodyPath, name)
		qriCmd.Stderr = os.Stderr
		qriCmd.Stdout = os.Stdout
		if err := qriCmd.Run(); err != nil {
			fmt.Printf("qri error: %s\n", err.Error())
			return
		}
	},
}

ImportCmd brings an open knowledge datapackage into Qri

View Source
var RootCmd = &cobra.Command{
	Short: "open knowledge foundation datapackage qri integration",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		if debug, err := cmd.Flags().GetBool("debug"); err == nil && debug {
			log.SetLevel(logrus.DebugLevel)
		}
	},
}

RootCmd is the walk command

Functions

func DatasetToDataPackage

func DatasetToDataPackage(ds *dataset.Dataset) (pkg *datapackage.Package, err error)

DatasetToDataPackage converts a dataset

func Execute

func Execute()

Execute adds all child commands to the root command sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL