refactor: Merge stbak and stcache commands

This commit is contained in:
Felicitas Pojtinger
2021-11-21 13:39:14 +01:00
parent 57f524cc21
commit aefbe4bbce
5 changed files with 17 additions and 80 deletions
@@ -7,7 +7,6 @@ import (
"io" "io"
"math" "math"
"os" "os"
"path/filepath"
"github.com/pojntfx/stfs/pkg/controllers" "github.com/pojntfx/stfs/pkg/controllers"
"github.com/pojntfx/stfs/pkg/formatting" "github.com/pojntfx/stfs/pkg/formatting"
@@ -17,14 +16,6 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const (
dbFlag = "db"
recordSizeFlag = "record-size"
recordFlag = "record"
blockFlag = "block"
overwriteFlag = "overwrite"
)
var indexCmd = &cobra.Command{ var indexCmd = &cobra.Command{
Use: "index", Use: "index",
Aliases: []string{"i"}, Aliases: []string{"i"},
@@ -232,14 +223,6 @@ var indexCmd = &cobra.Command{
} }
func init() { func init() {
// Get default working dir
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
workingDirDefault := filepath.Join(home, ".local", "share", "stcache", "var", "lib", "stcache")
indexCmd.PersistentFlags().StringP(dbFlag, "d", filepath.Join(workingDirDefault, "index.sqlite"), "Database to use")
indexCmd.PersistentFlags().IntP(recordSizeFlag, "e", 20, "Amount of 512-bit blocks per record") indexCmd.PersistentFlags().IntP(recordSizeFlag, "e", 20, "Amount of 512-bit blocks per record")
indexCmd.PersistentFlags().IntP(recordFlag, "r", 0, "Record to seek too before counting") indexCmd.PersistentFlags().IntP(recordFlag, "r", 0, "Record to seek too before counting")
indexCmd.PersistentFlags().IntP(blockFlag, "b", 0, "Block in record to seek too before counting") indexCmd.PersistentFlags().IntP(blockFlag, "b", 0, "Block in record to seek too before counting")
@@ -4,8 +4,6 @@ import (
"archive/tar" "archive/tar"
"context" "context"
"encoding/json" "encoding/json"
"os"
"path/filepath"
"github.com/pojntfx/stfs/pkg/formatting" "github.com/pojntfx/stfs/pkg/formatting"
"github.com/pojntfx/stfs/pkg/persisters" "github.com/pojntfx/stfs/pkg/persisters"
@@ -13,10 +11,10 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
var list = &cobra.Command{ var queryCmd = &cobra.Command{
Use: "list", Use: "query",
Aliases: []string{"l"}, Aliases: []string{"q"},
Short: "List contents of index", Short: "Query the contents of an index",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil { if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err return err
@@ -71,16 +69,7 @@ var list = &cobra.Command{
} }
func init() { func init() {
// Get default working dir
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
workingDirDefault := filepath.Join(home, ".local", "share", "stcache", "var", "lib", "stcache")
list.PersistentFlags().StringP(dbFlag, "d", filepath.Join(workingDirDefault, "index.sqlite"), "Database to use")
viper.AutomaticEnv() viper.AutomaticEnv()
rootCmd.AddCommand(list) rootCmd.AddCommand(queryCmd)
} }
+12 -1
View File
@@ -1,6 +1,8 @@
package cmd package cmd
import ( import (
"os"
"path/filepath"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -9,12 +11,13 @@ import (
const ( const (
tapeFlag = "tape" tapeFlag = "tape"
dbFlag = "db"
) )
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "stbak", Use: "stbak",
Short: "Simple Tape Backup", Short: "Simple Tape Backup",
Long: `Simple Tape Backup (stbak) is a CLI to interact with STFS-managed tapes or tar files. Long: `Simple Tape Backup (stbak) is a CLI to interact with STFS-managed tapes, tar files and indexes.
Find more information at: Find more information at:
https://github.com/pojntfx/stfs`, https://github.com/pojntfx/stfs`,
@@ -25,7 +28,15 @@ https://github.com/pojntfx/stfs`,
} }
func Execute() { func Execute() {
// Get default working dir
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
workingDirDefault := filepath.Join(home, ".local", "share", "stcache", "var", "lib", "stcache")
rootCmd.PersistentFlags().StringP(tapeFlag, "t", "/dev/nst0", "Tape or tar file to use") rootCmd.PersistentFlags().StringP(tapeFlag, "t", "/dev/nst0", "Tape or tar file to use")
rootCmd.PersistentFlags().StringP(dbFlag, "d", filepath.Join(workingDirDefault, "index.sqlite"), "Database to use")
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil { if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
panic(err) panic(err)
-39
View File
@@ -1,39 +0,0 @@
package cmd
import (
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
tapeFlag = "tape"
)
var rootCmd = &cobra.Command{
Use: "stcache",
Short: "Simple Tape Cache",
Long: `Simple Tape Cache (stcache) is a CLI to interact with STFS-managed indexes of tapes or tar files.
Find more information at:
https://github.com/pojntfx/stfs`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
viper.SetEnvPrefix("stcache")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
},
}
func Execute() {
rootCmd.PersistentFlags().StringP(tapeFlag, "t", "/dev/nst0", "Tape or tar file to use")
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
panic(err)
}
viper.AutomaticEnv()
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
-7
View File
@@ -1,7 +0,0 @@
package main
import "github.com/pojntfx/stfs/cmd/stcache/cmd"
func main() {
cmd.Execute()
}