-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtemplate.go
executable file
·129 lines (117 loc) · 2.56 KB
/
template.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
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
///bin/sh -c true; exec /usr/bin/env go run "$0" "$@"
// vim:ts=4:sts=4:sw=4:noet
//
// Author: Hari Sekhon
// Date: [% DATE # Wed Apr 29 15:16:09 2020 +0100 %]
//
// [% URL %]
//
// [% LICENSE %]
//
// [% MESSAGE %]
//
// [% LINKEDIN %]
//
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
//"log"
log "github.com/sirupsen/logrus"
"path"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
)
const description = `
Tool to do XXX
Tested on Mac OS X and Linux
`
var prog = path.Base(os.Args[0])
func readline() string {
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if err != nil {
log.Fatal(err)
}
line = strings.TrimSpace(line)
return line
}
func promptFloat(msg string) float64 {
fmt.Printf("%s: ", msg)
in := bufio.NewReader(os.Stdin)
line, err := in.ReadString('\n')
if err != nil {
log.Fatal(err)
}
line = strings.TrimSpace(line)
userFloat, err := strconv.ParseFloat(line, 64)
if err != nil {
log.Fatal(err)
}
return userFloat
}
func listToSlice(list []int) []int {
slice := make([]int, 0)
for _, v := range list {
num, err := strconv.ParseInt(v, 10, 32) // append(slice, int) requires 32-bit ints
if err != nil {
fmt.Printf("ERROR: %s is not a valid integer, ignoring\n", v)
continue
}
var num32 int = int(num)
slice = append(slice, num32)
// sort.Ints(slice)
}
return slice
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s\n\nusage: %s [options]\n\n", description, prog)
flag.PrintDefaults()
os.Exit(3)
}
var debug = flag.Bool("D", false, "Debug mode")
flag.Parse()
if *debug || os.Getenv("DEBUG") != "" {
log.SetLevel(log.DebugLevel)
log.Debug("debug logging enabled")
}
fmt.Printf("Enter a string: ")
line := readline()
list := strings.Split(line, " ")
nameAddress := map[string]string{"name": name, "address": address}
jsonData, err := json.Marshal(nameAddress)
if len(os.Args) > 1 {
filename := os.Args[1]
filehandle, err := os.Open(filename)
if err != nil {
//fmt.Fprintf(os.Stderr, "error: %s\n", err)
//os.Exit(1)
log.Fatal(err)
}
defer filehandle.Close()
scanner = bufio.NewScanner(filehandle)
} else {
scanner = bufio.NewScanner(os.Stdin)
}
scanner := bufio.NewScanner(filehandle)
for scanner.Scan() {
line := scanner.Text()
s := strings.Split(line, " ")
firstname, lastname := s[0], strings.Join(s[1:], " ")
//log.Printf("fname = %s", firstname)
//log.Printf("lname = %s", lastname)
p := Person{fname: firstname, lname: lastname}
sli = append(sli, p)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}