Skip to content

Commit

Permalink
Merge pull request #14 from TheYkk/master
Browse files Browse the repository at this point in the history
Seperate delete feature
  • Loading branch information
jack5341 authored Oct 3, 2021
2 parents b650df5 + 31f492f commit b1494d5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// decCmd represents the decrypt command
var decCmd = &cobra.Command{
Use: "dec",
Short: "List your all encrypted files and notes.",
Short: "Decrypt your files and notes.",
Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name")
var err error
Expand Down
55 changes: 55 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"errors"
"fmt"
"github.com/spf13/cobra"
"log"
"os"
)

// listCmd represents the list command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete your files or notes.",
Example: "dollop delete -n my-note\n# Or \ndollop delete -f my-file",
Run: func(cmd *cobra.Command, args []string) {
isNote, _ := cmd.Flags().GetBool("note")
isFile, _ := cmd.Flags().GetBool("file")

if args[0] == "" {
log.Fatal("You need to provide file name or note name")
}

name := "/"
if isNote {
name += "note/"
} else if isFile {
name += "files/"
}
name += args[0] + ".asc"

err := deleteObject(name)
if err != nil {
log.Fatal(err)
}
},
}

func init() {
rootCmd.AddCommand(deleteCmd)
deleteCmd.Flags().BoolP("note", "n", true, "--note")
deleteCmd.Flags().BoolP("file", "f", false, "--file")
}

func deleteObject(name string) error {
bucketName := os.Getenv("MINIO_BUCKET_NAME")

err := Client.RemoveObject(bucketName, name)
if err != nil {
return errors.New("failed to remove object")
}
fmt.Println("removed successfully")

return nil
}
27 changes: 4 additions & 23 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"fmt"
"log"
"os"
Expand All @@ -16,38 +15,20 @@ var listCmd = &cobra.Command{
Use: "list",
Short: "List your all encrypted files and notes.",
Run: func(cmd *cobra.Command, args []string) {
delete, _ := cmd.Flags().GetString("delete")

var err error
if delete != "" {
err = list(delete)

if err != nil {
log.Fatal(err)
}
err := list()
if err != nil {
log.Fatal(err)
}

list("")
},
}

func init() {
rootCmd.AddCommand(listCmd)
listCmd.Flags().StringP("delete", "d", "", "--delete=name-of-note-or-file")
}

func list(name string) error {
func list() error {
bucketName := os.Getenv("MINIO_BUCKET_NAME")

if name != "" {
err := Client.RemoveObject(bucketName, name)
if err != nil {
return errors.New("failed to remove object")
}
fmt.Println("removed successfully")
return nil
}

// Notes
NoteList := Client.ListObjects(bucketName, "notes", true, nil)

Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package main

import (
"github.com/jack5341/super-dollop/cmd"
"log"
)

func init() {
log.SetFlags(log.Lshortfile)
}

func main() {
cmd.Execute()
}

0 comments on commit b1494d5

Please sign in to comment.