-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsloth.go
More file actions
108 lines (93 loc) · 2.14 KB
/
sloth.go
File metadata and controls
108 lines (93 loc) · 2.14 KB
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
package main
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
)
// task template
type task struct {
TaskID string
TaskMsg string
TaskTime string
}
// get message from the main and
// convert it into slice to string
func getMessage(msg []string) (stringMsg string) {
stringMsg = strings.Join(msg, "")
return stringMsg
}
// get unix timestamp for an event
// converted into string
func getTimestamp() string {
unixTime := time.Now().Unix()
return strconv.FormatInt(unixTime, 10)
}
// construct hash value from the message and
// the timestamp of the instance we are created
func constructHashAllother(getMsg []string) (hash, strMsg, strTimestamp string) {
// get msg and the timestamp
strMsg = getMessage(getMsg)
strTimestamp = getTimestamp()
hashVictim := strMsg + strTimestamp
hasher := sha1.New()
hasher.Write([]byte(hashVictim))
finalhash := hasher.Sum(nil)
hash = hex.EncodeToString(finalhash)
return
}
// create new task
func newTask(rawMsg []string) *task {
p := new(task)
hash, msg, timestamp := constructHashAllother(rawMsg)
p.TaskID = hash
p.TaskMsg = msg
p.TaskTime = timestamp
return p
}
// add the task to list file
func addMsg(message []string) {
taskPtr := newTask(message)
bs, err := json.Marshal(taskPtr)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(bs))
}
func main() {
// subcommands
addCommand := flag.NewFlagSet("add", flag.ExitOnError)
// add subcommand pointer.
addCommandPtr := addCommand.String("m", "", "To specify the task message")
// To verify the subcommand is provided.
if len(os.Args) < 2 {
fmt.Println("Two more subcommand is required.")
os.Exit(1)
}
// Parse the flag for appropriate flagSet
// Subcommand parsing.
switch os.Args[1] {
case "add":
addCommand.Parse(os.Args[2:])
default:
flag.PrintDefaults()
os.Exit(1)
}
// Check the flagset for subcommand is parsed
// Check the false for the subcommand value
if addCommand.Parsed() {
if *addCommandPtr == "" {
addCommand.PrintDefaults()
os.Exit(1)
}
}
//command line message
message := os.Args[3:]
// get the message from the prompt
addMsg(message)
}