From 8d4e0ec138194d7dade8e5e86d0d0a61fc33798a Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 18 Nov 2017 14:58:56 -0500 Subject: [PATCH] Correctly name fileutils package This changes the containing dir name to match the fileutils package name. --- cmd/writeas/posts.go | 2 +- fileutils/fileutils.go | 107 +++++++++++++++++++++++++++++++++++++++++++++++++ fileutils/tempfile.go | 67 +++++++++++++++++++++++++++++++ utils/fileutils.go | 107 ------------------------------------------------- utils/tempfile.go | 67 ------------------------------- 5 files changed, 175 insertions(+), 175 deletions(-) create mode 100644 fileutils/fileutils.go create mode 100644 fileutils/tempfile.go delete mode 100644 utils/fileutils.go delete mode 100644 utils/tempfile.go diff --git a/cmd/writeas/posts.go b/cmd/writeas/posts.go index 60f72bf..8150570 100644 --- a/cmd/writeas/posts.go +++ b/cmd/writeas/posts.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "github.com/writeas/writeas-cli/utils" + "github.com/writeas/writeas-cli/fileutils" "io/ioutil" "os" "path/filepath" diff --git a/fileutils/fileutils.go b/fileutils/fileutils.go new file mode 100644 index 0000000..ba36356 --- /dev/null +++ b/fileutils/fileutils.go @@ -0,0 +1,107 @@ +package fileutils + +import ( + "bufio" + "fmt" + "os" + "strings" +) + +// Exists returns whether or not the given file exists +func Exists(p string) bool { + if _, err := os.Stat(p); err == nil { + return true + } + return false +} + +// WriteData writes data to the given path, creating the file if necessary. +func WriteData(path string, data []byte) { + f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600) + if err != nil { + fmt.Println(err) + } + // TODO: check for Close() errors + // https://github.com/ncw/swift/blob/master/swift.go#L170 + defer f.Close() + + _, err = f.Write(data) + if err != nil { + fmt.Println(err) + } +} + +// ReadData returns file data as an array of lines from the file. +func ReadData(p string) *[]string { + f, err := os.Open(p) + if err != nil { + return nil + } + defer f.Close() + + lines := []string{} + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + + if err := scanner.Err(); err != nil { + return nil + } + + return &lines +} + +// RemoveLine searches for the line that starts with the given value and, +// if found, removes it and saves the updated file. +func RemoveLine(p, startsWith string) { + f, err := os.Open(p) + if err != nil { + return + } + defer f.Close() + + var outText string + found := false + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + if strings.HasPrefix(scanner.Text(), startsWith) { + found = true + } else { + outText += scanner.Text() + string('\n') + } + } + + if err := scanner.Err(); err != nil { + return + } + + if found { + WriteData(p, []byte(outText)) + } +} + +// FindLine searches the given file for a line that begins with the given +// string. +func FindLine(p, startsWith string) string { + f, err := os.Open(p) + if err != nil { + return "" + } + defer f.Close() + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + if strings.HasPrefix(scanner.Text(), startsWith) { + return scanner.Text() + } + } + + if err := scanner.Err(); err != nil { + return "" + } + + return "" +} diff --git a/fileutils/tempfile.go b/fileutils/tempfile.go new file mode 100644 index 0000000..f99629e --- /dev/null +++ b/fileutils/tempfile.go @@ -0,0 +1,67 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fileutils + +import ( + "os" + "path/filepath" + "strconv" + "sync" + "time" +) + +// Random number state. +// We generate random temporary file names so that there's a good +// chance the file doesn't exist yet - keeps the number of tries in +// TempFile to a minimum. +var rand uint32 +var randmu sync.Mutex + +func reseed() uint32 { + return uint32(time.Now().UnixNano() + int64(os.Getpid())) +} + +func nextSuffix() string { + randmu.Lock() + r := rand + if r == 0 { + r = reseed() + } + r = r*1664525 + 1013904223 // constants from Numerical Recipes + rand = r + randmu.Unlock() + return strconv.Itoa(int(1e9 + r%1e9))[1:] +} + +// TempFile creates a new temporary file in the directory dir +// with a name beginning with prefix and ending with ext, opens the +// file for reading and writing, and returns the resulting *os.File. +// If dir is the empty string, TempFile uses the default directory +// for temporary files (see os.TempDir). +// Multiple programs calling TempFile simultaneously +// will not choose the same file. The caller can use f.Name() +// to find the pathname of the file. It is the caller's responsibility +// to remove the file when no longer needed. +func TempFile(dir, prefix, ext string) (f *os.File, err error) { + if dir == "" { + dir = os.TempDir() + } + + nconflict := 0 + for i := 0; i < 10000; i++ { + name := filepath.Join(dir, prefix+nextSuffix()+"."+ext) + f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) + if os.IsExist(err) { + if nconflict++; nconflict > 10 { + randmu.Lock() + rand = reseed() + randmu.Unlock() + } + continue + } + break + } + return +} diff --git a/utils/fileutils.go b/utils/fileutils.go deleted file mode 100644 index ba36356..0000000 --- a/utils/fileutils.go +++ /dev/null @@ -1,107 +0,0 @@ -package fileutils - -import ( - "bufio" - "fmt" - "os" - "strings" -) - -// Exists returns whether or not the given file exists -func Exists(p string) bool { - if _, err := os.Stat(p); err == nil { - return true - } - return false -} - -// WriteData writes data to the given path, creating the file if necessary. -func WriteData(path string, data []byte) { - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600) - if err != nil { - fmt.Println(err) - } - // TODO: check for Close() errors - // https://github.com/ncw/swift/blob/master/swift.go#L170 - defer f.Close() - - _, err = f.Write(data) - if err != nil { - fmt.Println(err) - } -} - -// ReadData returns file data as an array of lines from the file. -func ReadData(p string) *[]string { - f, err := os.Open(p) - if err != nil { - return nil - } - defer f.Close() - - lines := []string{} - - scanner := bufio.NewScanner(f) - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - - if err := scanner.Err(); err != nil { - return nil - } - - return &lines -} - -// RemoveLine searches for the line that starts with the given value and, -// if found, removes it and saves the updated file. -func RemoveLine(p, startsWith string) { - f, err := os.Open(p) - if err != nil { - return - } - defer f.Close() - - var outText string - found := false - - scanner := bufio.NewScanner(f) - for scanner.Scan() { - if strings.HasPrefix(scanner.Text(), startsWith) { - found = true - } else { - outText += scanner.Text() + string('\n') - } - } - - if err := scanner.Err(); err != nil { - return - } - - if found { - WriteData(p, []byte(outText)) - } -} - -// FindLine searches the given file for a line that begins with the given -// string. -func FindLine(p, startsWith string) string { - f, err := os.Open(p) - if err != nil { - return "" - } - defer f.Close() - - scanner := bufio.NewScanner(f) - for scanner.Scan() { - if strings.HasPrefix(scanner.Text(), startsWith) { - return scanner.Text() - } - } - - if err := scanner.Err(); err != nil { - return "" - } - - return "" -} diff --git a/utils/tempfile.go b/utils/tempfile.go deleted file mode 100644 index f99629e..0000000 --- a/utils/tempfile.go +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright 2010 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package fileutils - -import ( - "os" - "path/filepath" - "strconv" - "sync" - "time" -) - -// Random number state. -// We generate random temporary file names so that there's a good -// chance the file doesn't exist yet - keeps the number of tries in -// TempFile to a minimum. -var rand uint32 -var randmu sync.Mutex - -func reseed() uint32 { - return uint32(time.Now().UnixNano() + int64(os.Getpid())) -} - -func nextSuffix() string { - randmu.Lock() - r := rand - if r == 0 { - r = reseed() - } - r = r*1664525 + 1013904223 // constants from Numerical Recipes - rand = r - randmu.Unlock() - return strconv.Itoa(int(1e9 + r%1e9))[1:] -} - -// TempFile creates a new temporary file in the directory dir -// with a name beginning with prefix and ending with ext, opens the -// file for reading and writing, and returns the resulting *os.File. -// If dir is the empty string, TempFile uses the default directory -// for temporary files (see os.TempDir). -// Multiple programs calling TempFile simultaneously -// will not choose the same file. The caller can use f.Name() -// to find the pathname of the file. It is the caller's responsibility -// to remove the file when no longer needed. -func TempFile(dir, prefix, ext string) (f *os.File, err error) { - if dir == "" { - dir = os.TempDir() - } - - nconflict := 0 - for i := 0; i < 10000; i++ { - name := filepath.Join(dir, prefix+nextSuffix()+"."+ext) - f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) - if os.IsExist(err) { - if nconflict++; nconflict > 10 { - randmu.Lock() - rand = reseed() - randmu.Unlock() - } - continue - } - break - } - return -}