Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
- drop config file support (there's no config)
- merge PreRunE with Run options for sync command
- remove placeholder descriptions
- remove unused code
  • Loading branch information
dmke committed Aug 20, 2024
1 parent e72f517 commit e1981b6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/
/dist
zfs-snapback
46 changes: 2 additions & 44 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,15 @@ import (
"fmt"
"os"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var cfgFile string

Check failure on line 24 in cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

var `cfgFile` is unused (unused)

Check failure on line 24 in cmd/root.go

View workflow job for this annotation

GitHub Actions / lint

var `cfgFile` is unused (unused)

// RootCmd represents the base command when called without any subcommands
// RootCmd represents the base command when called without any subcommands.
var RootCmd = &cobra.Command{
Use: "zfs-snapback",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Use: "zfs-snapback",
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -50,39 +39,8 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.zfs-snapback.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search config in home directory with name ".zfs-snapback" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".zfs-snapback")
}

viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
55 changes: 9 additions & 46 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,33 @@ package cmd

import (
"fmt"
"os/exec"

"github.com/piotaixr/zfs-snapback/zfs"
"github.com/spf13/cobra"
)

// syncCmd represents the sync command
// syncCmd represents the sync command.
var syncCmd = &cobra.Command{
Use: "sync",
Short: "Synchronizes ZFS snapshots",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Args: cobra.ExactArgs(2),
Use: "sync",
Short: "Synchronizes ZFS snapshots",
Args: cobra.ExactArgs(2), //nolint:mnd
Example: "zfs-snapback sync [email protected]:zpool/var zpool/backup/remote.host",
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error

// source
source, err = zfs.GetFilesystem(flags, args[0])
RunE: func(cmd *cobra.Command, args []string) error {
source, err := zfs.GetFilesystem(flags, args[0])
if err != nil {
return fmt.Errorf("invalid source '%s': %w", args[0], err)
}

// source
destination, err = zfs.GetFilesystem(flags, args[1])
destination, err := zfs.GetFilesystem(flags, args[1])
if err != nil {
return fmt.Errorf("invalid destination '%s': %w", args[1], err)
}

return nil
},
Run: func(cmd *cobra.Command, args []string) {
checkError(zfs.DoSync(source, destination, flags))
return zfs.DoSync(source, destination, flags)
},
}

var (
flags zfs.Flags
source *zfs.Fs
destination *zfs.Fs
)
var flags zfs.Flags

func init() {
set := syncCmd.Flags()
Expand All @@ -70,23 +52,4 @@ func init() {
set.StringVarP(&flags.Compression, "compression", "c", "", "Set the compression option for SSH (yes/no)")

RootCmd.AddCommand(syncCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// syncCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// syncCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func checkError(err error) {
switch err := err.(type) {
case *exec.ExitError:
panic(string(err.Stderr))
case error:
panic(err)
}
}
15 changes: 3 additions & 12 deletions zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os/exec"
"slices"
"strconv"
"strings"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (z *Zfs) List() (*Fs, error) {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr != nil && len(exitErr.Stderr) > 0 {
// Add stderr to error message
err = fmt.Errorf("%s: %s", err, strings.TrimSpace(string(exitErr.Stderr)))
err = fmt.Errorf("%w: %s", exitErr, strings.TrimSpace(string(exitErr.Stderr)))
}

return nil, err
Expand Down Expand Up @@ -106,24 +107,14 @@ func lastCommonSnapshotIndex(listA, listB []string) int {
result := -1

for i, name := range listA {
if indexOf(listB, name) != -1 {
if slices.Index(listB, name) != -1 {
result = i
}
}

return result
}

func indexOf(list []string, needle string) int {
for i, e := range list {
if e == needle {
return i
}
}

return -1
}

func parseTransferSize(data []byte) (int64, error) {
buf := bytes.NewBuffer(data)
for {
Expand Down

0 comments on commit e1981b6

Please sign in to comment.