parent
43244302be
commit
335724cce7
@ -0,0 +1,49 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// forgetCmd represents the forget command
|
||||
var forgetCmd = &cobra.Command{
|
||||
Use: "forget",
|
||||
Short: "Forget and optionally prune snapshots according the specified policies",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
config := internal.GetConfig()
|
||||
if err := config.CheckConfig(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
{
|
||||
selected, err := internal.GetAllOrSelected(cmd, false)
|
||||
cobra.CheckErr(err)
|
||||
prune, _ := cmd.Flags().GetBool("prune")
|
||||
for _, name := range selected {
|
||||
location := config.Locations[name]
|
||||
err := location.Forget(prune)
|
||||
cobra.CheckErr(err)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(forgetCmd)
|
||||
internal.AddFlagsToCommand(forgetCmd, false)
|
||||
forgetCmd.Flags().Bool("prune", false, "Also prune repository")
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/cupcakearmy/autorestic/internal/bins"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var installCmd = &cobra.Command{
|
||||
Use: "install",
|
||||
Short: "A brief description of your command",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := bins.InstallRestic()
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(installCmd)
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
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 (
|
||||
"github.com/cupcakearmy/autorestic/internal/bins"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var upgradeCmd = &cobra.Command{
|
||||
Use: "upgrade",
|
||||
Short: "A brief description of your command",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
noRestic, _ := cmd.Flags().GetBool("no-restic")
|
||||
err := bins.Upgrade(!noRestic)
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(upgradeCmd)
|
||||
upgradeCmd.Flags().Bool("no-restic", false, "Also update restic. Default: true")
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
package bins
|
||||
|
||||
import (
|
||||
"compress/bzip2"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/blang/semver/v4"
|
||||
"github.com/cupcakearmy/autorestic/internal"
|
||||
)
|
||||
|
||||
const INSTALL_PATH = "/usr/local/bin"
|
||||
|
||||
type GithubReleaseAsset struct {
|
||||
Name string `json:"name"`
|
||||
Link string `json:"browser_download_url"`
|
||||
}
|
||||
type GithubRelease struct {
|
||||
Tag string `json:"tag_name"`
|
||||
Assets []GithubReleaseAsset `json:"assets"`
|
||||
}
|
||||
|
||||
func dlJSON(url string) (GithubRelease, error) {
|
||||
var parsed GithubRelease
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return parsed, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return parsed, err
|
||||
|
||||
}
|
||||
json.Unmarshal(body, &parsed)
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func InstallRestic() error {
|
||||
installed := internal.CheckIfCommandIsCallable("restic")
|
||||
if installed {
|
||||
fmt.Println("restic already installed")
|
||||
return nil
|
||||
} else {
|
||||
body, err := dlJSON("https://api.github.com/repos/restic/restic/releases/latest")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ending := fmt.Sprintf("_%s_%s.bz2", runtime.GOOS, runtime.GOARCH)
|
||||
for _, asset := range body.Assets {
|
||||
if strings.HasSuffix(asset.Name, ending) {
|
||||
// Found
|
||||
fmt.Println(asset.Link)
|
||||
|
||||
// Download archive
|
||||
resp, err := http.Get(asset.Link)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Uncompress
|
||||
bz := bzip2.NewReader(resp.Body)
|
||||
|
||||
// Save binary
|
||||
file, err := os.Create(path.Join(INSTALL_PATH, "restic"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
file.Chmod(0755)
|
||||
defer file.Close()
|
||||
io.Copy(file, bz)
|
||||
|
||||
fmt.Printf("Successfully installed restic under %s\n", INSTALL_PATH)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.New("could not find right binary for your system, please install restic manually. https://bit.ly/2Y1Rzai")
|
||||
}
|
||||
}
|
||||
|
||||
func upgradeRestic() error {
|
||||
out, err := internal.ExecuteCommand(internal.ExecuteOptions{
|
||||
Command: "restic",
|
||||
}, "self-update")
|
||||
fmt.Println(out)
|
||||
return err
|
||||
}
|
||||
|
||||
func Upgrade(restic bool) error {
|
||||
// Upgrade restic
|
||||
if restic {
|
||||
InstallRestic()
|
||||
upgradeRestic()
|
||||
}
|
||||
|
||||
// Upgrade self
|
||||
current, err := semver.ParseTolerant(internal.VERSION)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(current)
|
||||
|
||||
body, err := dlJSON("https://api.github.com/repos/cupcakearmy/autorestic/releases/latest")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
latest, err := semver.ParseTolerant(body.Tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if current.GT(latest) {
|
||||
|
||||
fmt.Println("Updated autorestic")
|
||||
} else {
|
||||
fmt.Println("Already up to date")
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in new issue