Skip to content

Commit

Permalink
Init CLI with cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
Themimitoof committed Mar 15, 2022
1 parent e3d4545 commit 1f43acf
Show file tree
Hide file tree
Showing 7 changed files with 929 additions and 10 deletions.
14 changes: 4 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
# Workspace specific
.vscode
.venv
venv*

# Python cache files
__pycache__
*.pyc
*.egg-info
dist/
# Ingore build files
build/
cambak*
!cambak.go
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2021-2022 Michael Vieira

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7 changes: 7 additions & 0 deletions cambak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/themimitoof/cambak/cmd"

func main() {
cmd.Execute()
}
65 changes: 65 additions & 0 deletions cmd/extract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
cb_config "github.com/themimitoof/cambak/config"
cb_files "github.com/themimitoof/cambak/files"
)

// extractCmd represents the extract command
var extractCmd = &cobra.Command{
Use: "extract",
Short: "Copy files from a source media to a local/remote destination",
Long: `The cambak extrator will copy/extract files from a source media (eg:
SD card, MTP drive, local/remote folder) to a local or remote destination folder.
By default, the folder destination structure is the following:
<destination folder>
└── <YEAR>
└── <MONTH>-<DAY>
└── <CAMERA_NAME>
├── Pictures
├── RAW
└── Movies
You can change the destination format by using the '--format' flag or change the value
in the configuration file.
For more information, please consult: https://github.com/themimitoof/cambak.`,
Aliases: []string{"copy", "cp"},
Args: func(cmd *cobra.Command, args []string) error {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("plop")
},
}

func init() {
rootCmd.AddCommand(extractCmd)

// File extraction selectors
extractCmd.Flags().BoolP("all", "A", false, "Import all medias files type")
extractCmd.Flags().BoolP("pictures", "P", false, "Import pictures files")
extractCmd.Flags().BoolP("raws", "R", false, "Import RAWs files")
extractCmd.Flags().BoolP("movies", "M", false, "Import movies files")
// extractCmd.Flags().StringP("date", "d", "", "Specify a date or a range to extract (not yet implemented)")
// extractCmd.Flags().StringP("rate", "r", "", "Specify a rate or a rating range to extract (not yet implemented)")

// Extraction collision management
extractCmd.Flags().BoolP("skip", "s", false, "Skip the source file if it already exists in the destination folder")
extractCmd.Flags().BoolP("merge", "m", false, "Merge the source file if it already exists in the destination folder")

extractCmd.Flags().StringP("format", "f", "", "Structure format in the destination folder.")
extractCmd.Flags().StringP("name", "n", "", "Name of the camera")

// Misc
extractCmd.Flags().Bool("dry-run", false, "Only log what the extractor will do if this flag was not set")
extractCmd.Flags().BoolP("clean", "c", false, "Delete source file after been copied")
// extractCmd.Flags().BoolP("auto-source", "a", false, "Auto guess the source file (not yet implemented)")
// extractCmd.Flags().BoolP("format-source", "z", false, "Format the source after the extraction done (not yet implemented)")
}
58 changes: 58 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/themimitoof/cambak/config"
)

var confPath string
var defaultConfPath string
var conf config.Configuration

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "cambak",
Short: "A simple but powerful tool for derushing cameras",
Long: `Cambak is a simple but powerful too for derushing cameras.
The program use a configuration file located in '$HOME/.config/cambak.yaml'.
During the first execution, a default configuration file will be created. You
can override it by an another configuration file by using the --config flag.
For more information, please consult: https://github.com/themimitoof/cambak.`,
}

// 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() {
// Get the $HOME value to build the value of the `defaultConfPath` var
environ := os.Environ()
homeFolder := "/tmp" // Just in case the $HOME env doesn't exists (for super sandboxed/weird envs)

for _, v := range environ {
splittedVal := strings.Split(v, "=")
key := splittedVal[0]
val := splittedVal[1]

if key == "HOME" {
homeFolder = val
break
}
}
defaultConfPath = homeFolder + "/.config/cambak.yaml"

// Specify global flags
rootCmd.PersistentFlags().StringVar(&confPath, "config", defaultConfPath, "Path of the configuration file")
}
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/themimitoof/cambak

go 1.17

require (
github.com/spf13/cobra v1.3.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
Loading

0 comments on commit 1f43acf

Please sign in to comment.