-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
85 lines (76 loc) · 2.33 KB
/
main.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
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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"runtime"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/ScienceSoft-Inc/integrity-sum/internal/logger"
"github.com/ScienceSoft-Inc/integrity-sum/internal/utils/graceful"
"github.com/ScienceSoft-Inc/integrity-sum/internal/walker"
"github.com/ScienceSoft-Inc/integrity-sum/internal/worker"
)
// Initializes the binding of the flag to a variable that must run before the main() function
func init() {
fsLog := pflag.NewFlagSet("log", pflag.ContinueOnError)
fsLog.StringP("verbose", "v", "info", "verbose level")
pflag.CommandLine.AddFlagSet(fsLog)
if err := viper.BindPFlags(fsLog); err != nil {
fmt.Println(err)
os.Exit(1)
}
fsSum := pflag.NewFlagSet("sum", pflag.ContinueOnError)
fsSum.Int("count-workers", runtime.NumCPU(), "number of running workers in the workerpool")
fsSum.String("algorithm", "SHA256", "algorithm MD5, SHA1, SHA224, SHA256, SHA384, SHA512, default: SHA256")
fsSum.String("dirPath", "./", "name of configMap for hasher")
fsSum.Bool("doHelp", false, "help")
pflag.CommandLine.AddFlagSet(fsSum)
if err := viper.BindPFlags(fsSum); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func main() {
pflag.Parse()
logger := logger.Init(viper.GetString("verbose"))
graceful.Execute(context.Background(), logger, func(ctx context.Context) {
switch {
case viper.GetBool("doHelp"):
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Custom help %s:\nYou can use the following flag:\n", os.Args[0])
flag.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(os.Stderr, " flag -%v \n %v\n", f.Name, f.Usage)
})
}
flag.Usage()
case len(viper.GetString("dirPath")) > 0:
run(ctx, logger)
default:
log.Println("use the -h flag on the command line to see all the flags in this app")
}
})
}
func run(ctx context.Context, log *logrus.Logger) {
hashesChan := worker.WorkersPool(
viper.GetInt("count-workers"),
walker.ChanWalkDir(ctx, []string{viper.GetString("dirPath")}, log),
worker.NewWorker(ctx, viper.GetString("algorithm"), log),
)
for {
select {
case hashData, ok := <-hashesChan:
if !ok {
log.Debug("completed")
return
}
fmt.Printf("%s %s\n", hashData.Hash, hashData.Path)
case <-ctx.Done():
fmt.Println("program termination after receiving a signal")
return
}
}
}