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

View File

@@ -7,7 +7,6 @@ import (
"io"
"math"
"os"
"path/filepath"
"github.com/pojntfx/stfs/pkg/controllers"
"github.com/pojntfx/stfs/pkg/formatting"
@@ -17,14 +16,6 @@ import (
"github.com/spf13/viper"
)
const (
dbFlag = "db"
recordSizeFlag = "record-size"
recordFlag = "record"
blockFlag = "block"
overwriteFlag = "overwrite"
)
var indexCmd = &cobra.Command{
Use: "index",
Aliases: []string{"i"},
@@ -232,14 +223,6 @@ var indexCmd = &cobra.Command{
}
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(recordFlag, "r", 0, "Record to seek too before counting")
indexCmd.PersistentFlags().IntP(blockFlag, "b", 0, "Block in record to seek too before counting")

View File

@@ -4,8 +4,6 @@ import (
"archive/tar"
"context"
"encoding/json"
"os"
"path/filepath"
"github.com/pojntfx/stfs/pkg/formatting"
"github.com/pojntfx/stfs/pkg/persisters"
@@ -13,10 +11,10 @@ import (
"github.com/spf13/viper"
)
var list = &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "List contents of index",
var queryCmd = &cobra.Command{
Use: "query",
Aliases: []string{"q"},
Short: "Query the contents of an index",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil {
return err
@@ -71,16 +69,7 @@ var list = &cobra.Command{
}
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()
rootCmd.AddCommand(list)
rootCmd.AddCommand(queryCmd)
}

View File

@@ -1,6 +1,8 @@
package cmd
import (
"os"
"path/filepath"
"strings"
"github.com/spf13/cobra"
@@ -9,12 +11,13 @@ import (
const (
tapeFlag = "tape"
dbFlag = "db"
)
var rootCmd = &cobra.Command{
Use: "stbak",
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:
https://github.com/pojntfx/stfs`,
@@ -25,7 +28,15 @@ https://github.com/pojntfx/stfs`,
}
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(dbFlag, "d", filepath.Join(workingDirDefault, "index.sqlite"), "Database to use")
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
panic(err)

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)
}
}

View File

@@ -1,7 +0,0 @@
package main
import "github.com/pojntfx/stfs/cmd/stcache/cmd"
func main() {
cmd.Execute()
}