Skip to content

Instantly share code, notes, and snippets.

@narqo
Created September 15, 2016 13:17
Show Gist options
  • Save narqo/8c265497a79ee0f85465ada4d0d60663 to your computer and use it in GitHub Desktop.
Save narqo/8c265497a79ee0f85465ada4d0d60663 to your computer and use it in GitHub Desktop.
A silly Go program to rename reports of golang show
// A program to rename reports from github.com/LK4D4/report
// See https://github.com/LK4D4/report/issues/2
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
const inPkg = "github.com/LK4D4/report/reports"
var (
GoPath = os.Getenv("GOPATH")
inDir = filepath.Join(GoPath, "src", inPkg)
)
func main() {
files, _ := ioutil.ReadDir(inDir)
for _, file := range files {
name := file.Name()
if file.IsDir() || !strings.HasPrefix(name, "golang-") {
continue
}
newName := newName(name)
os.Rename(filepath.Join(inDir, file.Name()), filepath.Join(inDir, newName))
}
}
func newName(name string) string {
ext := filepath.Ext(name)
splitted := strings.SplitN(
strings.TrimSuffix(strings.TrimPrefix(name, "golang-"), ext), "-", 2)
baseName := splitted[0]
var sfx string
if len(splitted) > 1 {
sfx = "_" + splitted[1]
}
t, _ := time.Parse("Jan2", baseName)
return fmt.Sprintf("golang-%02d-%02d%s%s", t.Month(), t.Day(), sfx, ext)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment