go rewrite

This commit is contained in:
2021-04-09 01:55:10 +02:00
parent 805bed7db1
commit 03cbbfd91c
41 changed files with 1149 additions and 2732 deletions

63
cmd/backup.go Normal file
View File

@@ -0,0 +1,63 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"github.com/cupcakearmy/autorestic/internal"
"github.com/cupcakearmy/autorestic/internal/lock"
"github.com/spf13/cobra"
)
// backupCmd represents the backup command
var backupCmd = &cobra.Command{
Use: "backup",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
config := internal.GetConfig()
{
err := config.CheckConfig()
cobra.CheckErr(err)
}
{
err := lock.Lock()
cobra.CheckErr(err)
}
defer lock.Unlock()
{
backup(internal.GetAllOrLocation(cmd, false), config)
}
},
}
func init() {
rootCmd.AddCommand(backupCmd)
backupCmd.PersistentFlags().StringSliceP("location", "l", []string{}, "Locations")
backupCmd.PersistentFlags().BoolP("all", "a", false, "Backup all locations")
}
func backup(locations []string, config *internal.Config) {
for _, name := range locations {
location, ok := config.Locations[name]
if !ok {
fmt.Println(fmt.Errorf("location `%s` does not exist", name))
} else {
fmt.Printf("Backing up: `%s`", name)
location.Backup()
}
}
}

41
cmd/check.go Normal file
View File

@@ -0,0 +1,41 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"errors"
"github.com/cupcakearmy/autorestic/internal"
"github.com/spf13/cobra"
)
// checkCmd represents the check command
var checkCmd = &cobra.Command{
Use: "check",
Short: "Check if everything is setup",
Run: func(cmd *cobra.Command, args []string) {
if !internal.CheckIfResticIsCallable() {
cobra.CheckErr(errors.New("restic is not callable. Install: https://restic.readthedocs.io/en/stable/020_installation.html"))
}
config := internal.GetConfig()
err := config.CheckConfig()
cobra.CheckErr(err)
},
}
func init() {
rootCmd.AddCommand(checkCmd)
}

50
cmd/exec.go Normal file
View File

@@ -0,0 +1,50 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"github.com/cupcakearmy/autorestic/internal"
"github.com/spf13/cobra"
)
// execCmd represents the exec command
var execCmd = &cobra.Command{
Use: "exec",
Short: "A brief description of your command",
Run: func(cmd *cobra.Command, args []string) {
config := internal.GetConfig()
if err := config.CheckConfig(); err != nil {
panic(err)
}
exec(internal.GetAllOrLocation(cmd, true), config, args)
},
}
func init() {
rootCmd.AddCommand(execCmd)
execCmd.PersistentFlags().StringSliceP("backend", "b", []string{}, "backends")
execCmd.PersistentFlags().BoolP("all", "a", false, "Exec in all backends")
}
func exec(backends []string, config *internal.Config, args []string) {
for _, name := range backends {
fmt.Println(name)
backend := config.Backends[name]
backend.Exec(args)
}
}

80
cmd/root.go Normal file
View File

@@ -0,0 +1,80 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"github.com/cupcakearmy/autorestic/internal"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "autorestic",
Short: "CLI Wrapper for restic",
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
cobra.CheckErr(rootCmd.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/.autorestic.yml or ./.autorestic.yml)")
// 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()
cobra.CheckErr(err)
viper.AddConfigPath(".")
viper.AddConfigPath(home)
viper.SetConfigName(".autorestic")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
internal.GetConfig()
}