Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cobra and viper setup #3

Merged
21 commits merged into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
# Go workspace file
go.work

# Build files
/out

### Helm ###
# Chart dependencies
**/charts/*.tgz
Expand Down
32 changes: 32 additions & 0 deletions cmd/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2023 cluetec GmbH
*
* 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/spf13/cobra"
)

// backupCmd represents the backup command
var backupCmd = &cobra.Command{
Use: "backup",
Short: "Execute the backup.",
Long: "Execute the backup. Used config can be overridden by providing arguments.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("backup called")
},
}
42 changes: 42 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2023 cluetec GmbH
*
* 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 (
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "lb",
Short: "Perform backups from any source to any destination.",
Long: "Lifeboat is a general purpose backup tool which supports backups for arbitrary sources and destinations.",
}

// 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() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
rootCmd.AddCommand(backupCmd)
}
31 changes: 31 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module github.com/cluetec/lifeboat

go 1.21.3

require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
505 changes: 505 additions & 0 deletions go.sum

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2023 cluetec GmbH
*
* 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 config

import (
"log/slog"
"strings"

"github.com/spf13/viper"
)

func InitViper() error {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AddConfigPath(".")
viper.SetConfigFile("./config.yaml")

if err := viper.ReadInConfig(); err != nil {
slog.Error("error while reading in the configs: %w", err)
return err
}

return nil
}
55 changes: 27 additions & 28 deletions internal_open_discussion.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

## Planned features

- Step: 1
- Init cobra & viper setup
- Dynamic config loading with viper (`type`)
- Step: 2
- Init slog setup
- Implement Filesystem to Filesystem backup
- Think about the interfaces
- Implement Vault to Filesystem backup
- [ ] Step: 1
- [x] Init cobra & viper setup
- [ ] Dynamic config loading with viper (`type`)
- [ ] Step: 2
- [ ] Init slog setup
- [ ] Implement Filesystem to Filesystem backup
- [ ] Think about the interfaces
- [ ] Implement Vault to Filesystem backup

For later:

- Define multiple destinations
- Azure Blob
- Support additional auth mechanism
- Creating full-backups from
- SSH/Rsync
- MongoDB
- PostgreSQL
- Disk
- Delete backups that are older than X
- Create incremental backups from
- MongoDB
- PostgreSQL
- Define multiple sources
- K8s Volume replication
- Encryption
- Compression
- Implement command `check-config`
- Implement command `generate-config`
- [ ] Define multiple destinations
- [ ] Azure Blob
- [ ] Support additional auth mechanism
- [ ] Creating full-backups from
- [ ] SSH/Rsync
- [ ] MongoDB
- [ ] PostgreSQL
- [ ] Disk
- [ ] Delete backups that are older than X
- [ ] Create incremental backups from
- [ ] MongoDB
- [ ] PostgreSQL
- [ ] Define multiple sources
- [ ] K8s Volume replication
- [ ] Encryption
- [ ] Compression
- [ ] Implement command `check-config`
- [ ] Implement command `generate-config`

## Config ideas

Expand All @@ -40,7 +40,6 @@ source:
address: http://localhost:8200
token: xxx


destination:
type: azureBlob
containerName: backup
Expand All @@ -53,6 +52,7 @@ lb backup
```

---

```yaml
sources:
production-env:
Expand Down Expand Up @@ -87,7 +87,6 @@ sources:
address: http://localhost:8200
token: xxx


destination:
type: azureBlob
name: backup
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 cluetec GmbH
*
* 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 main

import "github.com/cluetec/lifeboat/cmd"

func main() {
cmd.Execute()
}