~romuloalves/crypt-transfer

crypt-transfer/transfer.go -rw-r--r-- 2.5 KiB
e4596ffb — romuloalves Adds Gitlab to known_hosts during build 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main

import (
	"fmt"
	"log"
	"os"
	"path"
	"time"

	"git.sr.ht/romuloalves/crypt-transfer/copy"
	"github.com/pkg/errors"
	"github.com/urfave/cli/v2"
)

func getFromAndToPaths(c *cli.Context) (string, string, error) {
	if c.NArg() < 2 {
		return "", "", errors.New("Not enough args")
	}

	args := c.Args().Slice()

	return args[0], args[1], nil
}

func isPath(p string) bool {
	dir, file := path.Split(p)

	return dir != "" || file != ""
}

func main() {
	app := &cli.App{
		Name:     "crypt-transfer",
		Usage:    "Transfers files encrypting/decrypting them!",
		Version:  "v0.1.0",
		Compiled: time.Now(),
		Authors: []*cli.Author{
			{
				Name:  "romuloalves",
				Email: "me@romuloalves.dev",
			},
		},
		ArgsUsage: "[from] [to]",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "key",
				Aliases: []string{"k"},
				Usage:   "`secret` to encrypt/decrypt the files",
			},
		},
		Commands: []*cli.Command{
			{
				Name:    "encrypt",
				Aliases: []string{"e"},
				Usage:   "Copies the files encrypting them",
				Action: func(c *cli.Context) error {
					key := c.String("key")
					if key == "" {
						return errors.New("secret key is required")
					}

					from, to, err := getFromAndToPaths(c)
					if err != nil {
						return err
					}

					fromIsPath := isPath(from)
					toIsPath := isPath(to)
					if !fromIsPath {
						return errors.New("ˋfromˋ path is invalid")
					}
					if !toIsPath {
						return errors.New("ˋtoˋ path is invalid")
					}

					params := copy.CopyParam{
						Encrypt: true,
					}

					if err := copy.ConfigureAndCopy(from, to, key, params); err != nil {
						return err
					}

					fmt.Printf("> Done!\n")

					return nil
				},
			},
			{
				Name:    "decrypt",
				Aliases: []string{"d"},
				Usage:   "Copies the files decrypting them",
				Action: func(c *cli.Context) error {
					key := c.String("key")
					if key == "" {
						return errors.New("secret key is required")
					}

					from, to, err := getFromAndToPaths(c)
					if err != nil {
						return err
					}

					fromIsPath := isPath(from)
					toIsPath := isPath(to)
					if !fromIsPath {
						return errors.New("ˋfromˋ path is invalid")
					}
					if !toIsPath {
						return errors.New("ˋtoˋ path is invalid")
					}

					params := copy.CopyParam{
						Encrypt: false,
					}

					if err := copy.ConfigureAndCopy(from, to, key, params); err != nil {
						return err
					}

					fmt.Printf("> Done!\n")

					return nil
				},
			},
		},
	}

	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}