-
Notifications
You must be signed in to change notification settings - Fork 0
/
checksum.go
58 lines (43 loc) · 1.16 KB
/
checksum.go
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
// Copyright (c) 2022 Fujitsu Limited
package volumectl
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/FujitsuResearch/data-sharing-controllers-on-k8s/pkg/util"
)
type checksumOptions struct {
filePath string
}
func newChecksumOptions() *checksumOptions {
return &checksumOptions{}
}
func addChecksumFlags(cmd *cobra.Command, co *checksumOptions) {
cmd.Flags().StringVar(
&co.filePath, "file-path", co.filePath,
"Path to a executable file to calculate the checksum")
cmd.MarkFlagRequired("file-path")
}
func newChecksumCommand() *cobra.Command {
opts := newChecksumOptions()
subCmd := &cobra.Command{
Use: "calc-checksum",
Long: `the shadowy volumectl calcuates a file checksum`,
Run: func(cmd *cobra.Command, args []string) {
util.PrintFlags(cmd.Flags())
start := time.Now()
checksum, err := util.CalculateChecksum(opts.filePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err.Error())
os.Exit(1)
}
elapsed := time.Since(start)
fmt.Fprintf(
os.Stdout, "[%v sec] Checksum for %s: %x\n",
elapsed.Seconds(), opts.filePath, checksum)
},
}
addChecksumFlags(subCmd, opts)
return subCmd
}