cmd

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCmd = &cobra.Command{
	Use:   "todots",
	Short: "todots is a simple CLI writen in Go to make a copy of your dotfiles.",
	RunE: func(cmd *cobra.Command, args []string) error {
		for k, v := range viper.AllSettings() {
			home, err := homedir.Dir()
			if err != nil {
				return fmt.Errorf("while trying to determine the HOME directory: %v", err)
			}

			srcPath := home + "/" + v.(string)
			if err := copier.Check(srcPath); err != nil {
				return fmt.Errorf("while checking source path %q: %v", srcPath, err)
			}

			dotFile := copier.NewDotfile(k)
			sf, err := os.Open(srcPath)
			if err != nil {
				return fmt.Errorf("while opening file on path %q: %v", srcPath, err)
			}
			defer sf.Close()

			r := bufio.NewReader(sf)
			if err := dotFile.CopyFrom(r); err != nil {
				return fmt.Errorf("while copying data from file %q: %v", sf.Name(), err)
			}

			_, fileName := filepath.Split(srcPath)
			dstPath := dstDir + "/" + fileName
			df, err := os.Create(dstPath)
			if err != nil {
				return fmt.Errorf("while creating destination file %q on %q: %v", fileName, dstPath, err)
			}
			defer df.Close()

			if _, err := dotFile.WriteTo(df); err != nil {
				return fmt.Errorf("while copying data from %q to destination file on %q: %v", sf.Name(), fileName, err)
			}

			if err := copier.Check(dstPath); err != nil {
				return fmt.Errorf("while checking destination path %q: %v", dstPath, err)
			}

			fmt.Printf("copy of %q successfully created.\n", fileName)
		}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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