1package activityserve 2 3import ( 4 "bufio" 5 "io" 6 "net/http" 7 "os" 8 9 // "net/url" 10 "bytes" 11 "encoding/json" 12 13 // "time" 14 // "fmt" 15 "github.com/gologme/log" 16 // "github.com/go-fed/httpsig" 17) 18 19func isSuccess(code int) bool { 20 return code == http.StatusOK || 21 code == http.StatusCreated || 22 code == http.StatusAccepted || 23 code == http.StatusNoContent 24} 25 26//PrettyPrint maps 27func PrettyPrint(themap map[string]interface{}) { 28 b, err := json.MarshalIndent(themap, "", " ") 29 if err != nil { 30 log.Info("error:", err) 31 } 32 log.Print(string(b)) 33} 34 35//PrettyPrintJSON does what it's name says 36func PrettyPrintJSON(theJSON []byte) { 37 dst := new(bytes.Buffer) 38 json.Indent(dst, theJSON, "", "\t") 39 log.Info(dst) 40} 41 42// FormatJSON formats json with tabs and 43// returns the new string 44func FormatJSON(theJSON []byte) string { 45 dst := new(bytes.Buffer) 46 json.Indent(dst, theJSON, "", "\t") 47 return dst.String() 48} 49 50// FormatHeaders to string for printing 51func FormatHeaders(header http.Header) string { 52 buf := new(bytes.Buffer) 53 header.Write(buf) 54 return buf.String() 55} 56 57func context() [1]string { 58 return [1]string{"https://www.w3.org/ns/activitystreams"} 59} 60 61// ReadLines reads specific lines from a file and returns them as 62// an array of strings 63func ReadLines(filename string, from, to int) (lines []string, err error) { 64 lines = make([]string, 0, to-from) 65 reader, err := os.Open(filename) 66 if err != nil { 67 log.Info("could not read file") 68 log.Info(err) 69 return 70 } 71 sc := bufio.NewScanner(reader) 72 line := 0 73 for sc.Scan() { 74 line++ 75 if line >= from && line <= to { 76 lines = append(lines, sc.Text()) 77 } 78 } 79 return lines, nil 80} 81 82func lineCounter(filename string) (int, error) { 83 r, err := os.Open(filename) 84 if err != nil { 85 log.Info("could not read file") 86 log.Info(err) 87 return 0, nil 88 } 89 buf := make([]byte, 32*1024) 90 count := 0 91 lineSep := []byte{'\n'} 92 93 for { 94 c, err := r.Read(buf) 95 count += bytes.Count(buf[:c], lineSep) 96 97 switch { 98 case err == io.EOF: 99 return count, nil 100 101 case err != nil: 102 return count, err 103 } 104 } 105} 106