<commit_before>package main

import (
	"bytes"
	"errors"
	"flag"
	"hash/crc32"
	"net/http"
	"os"
	"os/exec"
	"strings"
	"sync"

	"qiniupkg.com/http/httputil.v2"
	"qiniupkg.com/x/log.v7"
)

const (
	mutexCount = 9973
)

var (
	ErrUnmatchedInodeType = errors.New("unmatched inode type(file or dir)")
)

var (
	doxygenApp string

	dataRootDir string
	srcRootDir  string
	tmpRootDir  string

	mutexs [mutexCount]sync.Mutex
)

func handleHome(w http.ResponseWriter, req *http.Request) {

}

func handleUnknown(w http.ResponseWriter, req *http.Request) {

}

func handleMain(w http.ResponseWriter, req *http.Request) {

	path := req.URL.Path

	if path == "/" {
		handleHome(w, req)
		return
	}

	if strings.Index(path, "..") >= 0 {
		handleUnknown(w, req)
		return
	}

	parts := strings.SplitN(path[1:], "/", 4)
	if parts[0] != "github.com" || len(parts) < 3 {
		handleUnknown(w, req)
		return
	}

	pkg := strings.Join(parts[:3], "/")
	dataDir := dataRootDir + pkg
	htmlDir := dataDir + "/html/"
	err := isEntryExists(htmlDir, true)
	if err != nil {
		err = genDoc(parts, pkg, dataDir, htmlDir)
		if err != nil {
			httputil.Error(w, err)
			return
		}
	}

	if len(parts) > 3 {
		file := htmlDir + parts[3]
		if strings.HasSuffix(file, "/") {
			file += "index.html"
		}
		f, err := os.Open(file)
		if err != nil {
			httputil.Error(w, err)
			return
		}
		defer f.Close()
		fi, err := f.Stat()
		if err != nil {
			httputil.Error(w, err)
			return
		}
		serveContent(w, req, pkg, fi.Name(), fi.ModTime(), f)
	} else {
		http.Redirect(w, req, path + "/", 301)
	}
}

func genDoc(parts []string, pkg, dataDir, htmlDir string) (err error) {

	srcDir := srcRootDir + pkg
	repo := "https://github.com/" + parts[1] + "/" + parts[2] + ".git"

	crc := crc32.ChecksumIEEE([]byte(pkg))
	mutex := &mutexs[crc % mutexCount]
	mutex.Lock()
	defer mutex.Unlock()

	err2 := isEntryExists(htmlDir, true)
	if err2 != nil {
		err = cloneRepo(srcDir, repo)
		if err != nil {
			return
		}

		err = os.MkdirAll(dataDir, 0755)
		if err != nil {
			return
		}

		doxyfile := tmpRootDir + "github.com!" + parts[1] + "!" + parts[2] + ".doxyfile"
		err = genDoxyfileFile(doxyfile, &doxyfileConf{
			ProjectName:  parts[2],
			OutputDir:    dataDir,
			InputDir:     srcDir,
			FilePatterns: "*.java *.h *.hpp *.hxx *.py *.php *.rb *.cs *.js *.scala *.go *.lua *.asp",
		})
		if err != nil {
			return
		}

		err = runCmd(doxygenApp, doxyfile)
		if err != nil {
			return
		}
	}
	return
}

func cloneRepo(srcDir string, repo string) (err error) {

	os.RemoveAll(srcDir)
	err = os.MkdirAll(srcDir, 0755)
	if err != nil {
		return
	}

	return runCmd("git", "clone", "--depth=50", repo, srcDir)
}

func runCmd(command string, args ...string) (err error) {

	cmd := exec.Command(command, args...)

	var out bytes.Buffer
	cmd.Stderr = &out

	err = cmd.Run()
	if err == nil {
		return
	}

	emsg := out.String()
	if emsg != "" {
		return errors.New(emsg)
	}
	return err
}

func isEntryExists(entryPath string, isDir bool) (err error) {

	fi, err := os.Stat(entryPath)
	if err != nil {
		return
	}

	if fi.IsDir() != isDir {
		err = ErrUnmatchedInodeType
		return
	}
	return nil
}

var (
	bindHost = flag.String("http", ":8888", "address that doxygend server listen")
)

func main() {

	flag.Parse()

	rootDir := os.Getenv("HOME") + "/.doxygen.io/"
	doxygenApp = os.Getenv("DOXYGEN")
	if doxygenApp == "" {
		doxygenApp = "doxygen"
	}

	dataRootDir = rootDir + "data/"
	srcRootDir = rootDir + "src/"
	tmpRootDir = rootDir + "tmp/"
	os.MkdirAll(tmpRootDir, 0755)

	http.HandleFunc("/", handleMain)
	err := http.ListenAndServe(*bindHost, nil)
	if err != nil {
		log.Fatal(err)
	}
}

<commit_msg>support refresh<commit_after>package main

import (
	"bytes"
	"errors"
	"flag"
	"hash/crc32"
	"net/http"
	"os"
	"os/exec"
	"strings"
	"sync"
	"time"

	"qiniupkg.com/http/httputil.v2"
	"qiniupkg.com/x/log.v7"
)

const (
	mutexCount = 9973
)

var (
	ErrUnmatchedInodeType = errors.New("unmatched inode type(file or dir)")
	ErrRefreshWithoutPath = httputil.NewError(400, "refresh without path")
	ErrInvalidPkgPath     = httputil.NewError(400, "invalid package path")
)

var (
	doxygenApp string

	dataRootDir string
	srcRootDir  string
	tmpRootDir  string

	refreshRootDir string

	mutexs [mutexCount]sync.Mutex
)

func handleHome(w http.ResponseWriter, req *http.Request) {

}

func handleUnknown(w http.ResponseWriter, req *http.Request) {

}

func handleRefresh(w http.ResponseWriter, req *http.Request) {

	pkg := req.PostFormValue("path")
	if pkg == "" {
		httputil.Error(w, ErrRefreshWithoutPath)
		return
	}

	log.Info("Refresh", pkg)

	err := refresh(pkg)
	if err != nil {
		httputil.Error(w, err)
		return
	}

	http.Redirect(w, req, "/" + pkg + "/", 301)
}

func refresh(pkg string) (err error) {

	dataDir := dataRootDir + pkg
	indexFile := dataDir + "/html/index.html"
	if isRefreshed(indexFile) {
		return nil
	}

	if strings.Index(pkg, "..") >= 0 {
		return ErrInvalidPkgPath
	}

	parts := strings.SplitN(pkg, "/", 4)
	if len(parts) != 3 {
		return ErrInvalidPkgPath
	}

	refreshDir := refreshRootDir + pkg
	refreshHtmlDir := refreshDir + "/html/"
	os.RemoveAll(refreshDir)
	err = genDoc(parts, pkg, refreshDir, refreshHtmlDir)
	if err != nil {
		return
	}

	os.RemoveAll(dataDir)
	return os.Rename(refreshDir, dataDir)
}

func isRefreshed(indexFile string) bool {

	fi, err := os.Stat(indexFile)
	if err != nil {
		return false
	}

	return time.Now().Sub(fi.ModTime()) < 10*time.Second
}

func handleMain(w http.ResponseWriter, req *http.Request) {

	path := req.URL.Path

	if path == "/" {
		handleHome(w, req)
		return
	}

	log.Info("View", path)

	if strings.Index(path, "..") >= 0 {
		handleUnknown(w, req)
		return
	}

	parts := strings.SplitN(path[1:], "/", 4)
	if parts[0] != "github.com" || len(parts) < 3 {
		handleUnknown(w, req)
		return
	}

	pkg := strings.Join(parts[:3], "/")
	dataDir := dataRootDir + pkg
	htmlDir := dataDir + "/html/"
	err := isEntryExists(htmlDir, true)
	if err != nil {
		err = genDoc(parts, pkg, dataDir, htmlDir)
		if err != nil {
			httputil.Error(w, err)
			return
		}
	}

	if len(parts) > 3 {
		file := htmlDir + parts[3]
		if strings.HasSuffix(file, "/") {
			file += "index.html"
		}
		f, err := os.Open(file)
		if err != nil {
			httputil.Error(w, err)
			return
		}
		defer f.Close()
		fi, err := f.Stat()
		if err != nil {
			httputil.Error(w, err)
			return
		}
		serveContent(w, req, pkg, fi.Name(), fi.ModTime(), f)
	} else {
		http.Redirect(w, req, path + "/", 301)
	}
}

func genDoc(parts []string, pkg, dataDir, htmlDir string) (err error) {

	srcDir := srcRootDir + pkg
	repo := "https://github.com/" + parts[1] + "/" + parts[2] + ".git"

	crc := crc32.ChecksumIEEE([]byte(pkg))
	mutex := &mutexs[crc % mutexCount]
	mutex.Lock()
	defer mutex.Unlock()

	err2 := isEntryExists(htmlDir, true)
	if err2 != nil {
		err = cloneRepo(srcDir, repo)
		if err != nil {
			return
		}

		err = os.MkdirAll(dataDir, 0755)
		if err != nil {
			return
		}

		doxyfile := tmpRootDir + "github.com!" + parts[1] + "!" + parts[2] + ".doxyfile"
		err = genDoxyfileFile(doxyfile, &doxyfileConf{
			ProjectName:  parts[2],
			OutputDir:    dataDir,
			InputDir:     srcDir,
			FilePatterns: "*.java *.h *.hpp *.hxx *.py *.php *.rb *.cs *.js *.scala *.go *.lua *.asp",
		})
		if err != nil {
			return
		}

		err = runCmd(doxygenApp, doxyfile)
		if err != nil {
			return
		}
	}
	return
}

func cloneRepo(srcDir string, repo string) (err error) {

	os.RemoveAll(srcDir)
	err = os.MkdirAll(srcDir, 0755)
	if err != nil {
		return
	}

	return runCmd("git", "clone", "--depth=50", repo, srcDir)
}

func runCmd(command string, args ...string) (err error) {

	cmd := exec.Command(command, args...)

	var out bytes.Buffer
	cmd.Stderr = &out

	err = cmd.Run()
	if err == nil {
		return
	}

	emsg := out.String()
	if emsg != "" {
		return errors.New(emsg)
	}
	return err
}

func isEntryExists(entryPath string, isDir bool) (err error) {

	fi, err := os.Stat(entryPath)
	if err != nil {
		return
	}

	if fi.IsDir() != isDir {
		err = ErrUnmatchedInodeType
		return
	}
	return nil
}

var (
	bindHost = flag.String("http", ":8888", "address that doxygen.io server listen")
)

func main() {

	flag.Parse()

	rootDir := os.Getenv("HOME") + "/.doxygen.io/"
	doxygenApp = os.Getenv("DOXYGEN")
	if doxygenApp == "" {
		doxygenApp = "doxygen"
	}

	dataRootDir = rootDir + "data/"
	refreshRootDir = rootDir + "refresh/"
	srcRootDir = rootDir + "src/"
	tmpRootDir = rootDir + "tmp/"
	os.MkdirAll(tmpRootDir, 0755)

	http.HandleFunc("/-/refresh", handleRefresh)
	http.HandleFunc("/", handleMain)
	err := http.ListenAndServe(*bindHost, nil)
	if err != nil {
		log.Fatal(err)
	}
}

<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"sort"
	"time"

	"github.com/acstech/liquid"
)

// This is the longest label. Pull it out here so we can both use it, and measure it for alignment.
const configurationFileLabel = "Configuration file:"

func printSetting(label string, value string) {
	fmt.Printf("%s %s\n",
		leftPad(label, len(configurationFileLabel)), value)
}

func printPathSetting(label string, path string) {
	path, err := filepath.Abs(path)
	if err != nil {
		panic("Couldn't convert to absolute path")
	}
	printSetting(label, path)
}

func main() {
	liquid.Tags["link"] = LinkFactory

	flag.StringVar(&siteConfig.DestinationDir, "destination", siteConfig.DestinationDir, "Destination directory")
	flag.StringVar(&siteConfig.SourceDir, "source", siteConfig.SourceDir, "Source directory")

	// routes subcommand
	dynamic := flag.Bool("dynamic", false, "Dynamic routes only")

	flag.Parse()

	configPath := filepath.Join(siteConfig.SourceDir, "_config.yml")
	// TODO error if file is e.g. unreadable
	if _, err := os.Stat(configPath); err == nil {
		if err := siteConfig.read(configPath); err != nil {
			fmt.Println(err)
			return
		}
		printPathSetting(configurationFileLabel, configPath)
	} else {
		printSetting(configurationFileLabel, "none")
	}
	printPathSetting("Source:", siteConfig.SourceDir)
	printPathSetting("Destination:", siteConfig.DestinationDir)

	fileMap, err := buildSiteMap()
	if err != nil {
		fmt.Println(err)
		return
	}
	siteMap = fileMap

	switch flag.Arg(0) {
	case "s", "serve", "server":
		err = server()
		if err != nil {
			fmt.Println(err)
		}
	case "b", "build":
		printSetting("Generating...", "")
		start := time.Now()
		err = build()
		if err != nil {
			fmt.Println(err)
			break
		}
		elapsed := time.Since(start)
		printSetting("", fmt.Sprintf("done in %.2fs.", elapsed.Seconds()))
	case "routes":
		fmt.Printf("\nRoutes:\n")
		urls := []string{}
		for u, p := range siteMap {
			if !(*dynamic && p.Static) {
				urls = append(urls, u)
			}
		}
		sort.Strings(urls)
		for _, u := range urls {
			fmt.Printf("  %s -> %s\n", u, siteMap[u].Path)
		}
	case "render":
		// build a single page, and print it to stdout; for testing
		page, err2 := readFile("index.md", siteData, true)
		if err2 != nil {
			fmt.Println(err2)
			break
		}
		fmt.Println(string(page.Body))
	default:
		fmt.Println("A subcommand is required.")
	}
}
<commit_msg>Improvements to subcommand impls<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"time"

	"github.com/acstech/liquid"
)

// This is the longest label. Pull it out here so we can both use it, and measure it for alignment.
const configurationFileLabel = "Configuration file:"

func printSetting(label string, value string) {
	fmt.Printf("%s %s\n",
		leftPad(label, len(configurationFileLabel)), value)
}

func printPathSetting(label string, path string) {
	path, err := filepath.Abs(path)
	if err != nil {
		panic("Couldn't convert to absolute path")
	}
	printSetting(label, path)
}

func main() {
	liquid.Tags["link"] = LinkFactory

	flag.StringVar(&siteConfig.DestinationDir, "destination", siteConfig.DestinationDir, "Destination directory")
	flag.StringVar(&siteConfig.SourceDir, "source", siteConfig.SourceDir, "Source directory")

	// routes subcommand
	dynamic := flag.Bool("dynamic", false, "Dynamic routes only")

	flag.Parse()
	if len(flag.Args()) < 1 {
		fmt.Println("A subcommand is required.")
		return
	}

	configPath := filepath.Join(siteConfig.SourceDir, "_config.yml")
	// TODO error if file is e.g. unreadable
	if _, err := os.Stat(configPath); err == nil {
		if err := siteConfig.read(configPath); err != nil {
			fmt.Println(err)
			return
		}
		printPathSetting(configurationFileLabel, configPath)
	} else {
		printSetting(configurationFileLabel, "none")
	}
	printPathSetting("Source:", siteConfig.SourceDir)

	start := time.Now()
	fileMap, err := buildSiteMap()
	if err != nil {
		fmt.Println(err)
		return
	}
	siteMap = fileMap

	switch flag.Arg(0) {
	case "s", "serve", "server":
		if err = server(); err != nil {
			fmt.Println(err)
		}
	case "b", "build":
		printPathSetting("Destination:", siteConfig.DestinationDir)
		printSetting("Generating...", "")
		if err = build(); err != nil {
			fmt.Println(err)
			break
		}
		elapsed := time.Since(start)
		printSetting("", fmt.Sprintf("done in %.2fs.", elapsed.Seconds()))
	case "routes":
		fmt.Printf("\nRoutes:\n")
		urls := []string{}
		for u, p := range siteMap {
			if !(*dynamic && p.Static) {
				urls = append(urls, u)
			}
		}
		sort.Strings(urls)
		for _, u := range urls {
			fmt.Printf("  %s -> %s\n", u, siteMap[u].Path)
		}
	case "render":
		path := flag.Arg(1)
		if path == "" {
			path = "index.md"
		}
		if strings.HasPrefix(path, "/") {
			if page, found := siteMap[path]; found {
				path = page.Path
			} else {
				fmt.Println("No page at", path)
				return
			}
		}
		page, e := readFile(path, siteData, true)
		if e != nil {
			fmt.Println(e)
			break
		}
		printPathSetting("Render:", filepath.Join(siteConfig.SourceDir, path))
		printSetting("URL:", page.Permalink)
		fmt.Println(string(page.Body))
	default:
		fmt.Println("A subcommand is required.")
	}
}
<|endoftext|>


<commit_before>package main

import(
    "errors"
    "log"
    "os"
    "strings"
    "url"
    "encoding/json"
    "math/rand"
    "net/http"
    "net/url"

    "github.com/nlopes/slack"
)

var (
   atk = os.Getenv("AUTHENTIFICATION_TOKEN")
   vtk = os.Getenv("VERIFICATION_TOKEN")
   appURL = os.Getenv("APP_URL")
   iconURL = appURL + "/static/icon.png"
   memeURL = appURL + "/static/spongemock.jpg"
   api = slack.New(atk)
)

func transformText(m string) string {
    var buffer bytes.Buffer
    for i := 0; i < len(m); i++ {
        ch := m[i:i+1]
        if rand.Int() % 2 == 0 {
            ch = strings.ToLower(ch)
        }
        buffer.WriteString(ch)
    }
    return buffer.String()
}

func isValidSlackRequest(r *http.Request) bool {
    if r.Method != "POST" {
        log.Printf("want method POST, got %s\n", r.Method)
        return false
    }
    err := r.ParseForm()
    if err != nil {
        log.Printf("invalid form data: %s\n", err)
        return false
    }
    if cmd := r.PostFormValue("command"); cmd != "/spongemock" {
        log.Printf("want command /spongemock, got %s\n", cmd)
        return false
    }
    if tk := r.PostFormValue("token"); tk != vtk {
        log.Printf("received invalid token %s\n", tk)
        return false
    }
    if url := r.PostFormValue("response_url"); url == "" {
        log.Println("did not receive response url")
        return false
    }
    return true
}

func getLastSlackMessage(c string) (string, error) {
    h, err := api.GetChannelHistory(c, slack.NewHistoryParameters())
    if err != nil {
        log.Printf("history API request error: %s", err)
        return "", err
    }

    for _, msg := range h.Messages {
        if msg.Text == "" {
            continue
        }
        return msg.Text, nil
    }

    err = errors.New("no last message found")
    log.Println(err)
    return "", err
}

func handleSlack(w http.ResponseWriter, r *http.Request) {
    status := http.StatusOk
    defer func() {
        w.WriteHeader(status)
    }
    if !isValidSlackRequest(r) {
        status = http.StatusBadRequest
        return
    }
    channel := r.PostFormValue("channel_id")
    lastMessage, err := getLastSlackMessage(channel)
    if err != nil {
        status = http.StatusInternalServerError
        return
    }
    params := NewPostMessageParameters()
    params.Username = "Spongebob"
    params.Attachments = []slack.Attachment{{
        Text: transformText(lastMessage),
        Fallback: "*Spongebob mocking meme*",
        ImageURL: memeURL,
    }}
    params.IconURL = iconURL
    _, _, err := api.PostMessage(channel, "", params)
    if err != nil {
        status = http.StatusInternalServerError
    }
}

func main() {
}
<commit_msg>Ran gofmt<commit_after>package main

import (
	"bytes"
	"errors"
	"log"
	"math/rand"
	"net/http"
	"os"
	"strings"

	"github.com/nlopes/slack"
)

var (
	atk     = os.Getenv("AUTHENTIFICATION_TOKEN")
	vtk     = os.Getenv("VERIFICATION_TOKEN")
	appURL  = os.Getenv("APP_URL")
	iconURL = appURL + "/static/icon.png"
	memeURL = appURL + "/static/spongemock.jpg"
	api     = slack.New(atk)
)

func transformText(m string) string {
	var buffer bytes.Buffer
	for i := 0; i < len(m); i++ {
		ch := m[i : i+1]
		if rand.Int()%2 == 0 {
			ch = strings.ToLower(ch)
		}
		buffer.WriteString(ch)
	}
	return buffer.String()
}

func isValidSlackRequest(r *http.Request) bool {
	if r.Method != "POST" {
		log.Printf("want method POST, got %s\n", r.Method)
		return false
	}
	err := r.ParseForm()
	if err != nil {
		log.Printf("invalid form data: %s\n", err)
		return false
	}
	if cmd := r.PostFormValue("command"); cmd != "/spongemock" {
		log.Printf("want command /spongemock, got %s\n", cmd)
		return false
	}
	if tk := r.PostFormValue("token"); tk != vtk {
		log.Printf("received invalid token %s\n", tk)
		return false
	}
	if url := r.PostFormValue("response_url"); url == "" {
		log.Println("did not receive response url")
		return false
	}
	return true
}

func getLastSlackMessage(c string) (string, error) {
	h, err := api.GetChannelHistory(c, slack.NewHistoryParameters())
	if err != nil {
		log.Printf("history API request error: %s", err)
		return "", err
	}

	for _, msg := range h.Messages {
		if msg.Text == "" {
			continue
		}
		return msg.Text, nil
	}

	err = errors.New("no last message found")
	log.Println(err)
	return "", err
}

func handleSlack(w http.ResponseWriter, r *http.Request) {
	status := http.StatusOK
	defer func() {
		w.WriteHeader(status)
	}()
	if !isValidSlackRequest(r) {
		status = http.StatusBadRequest
		return
	}
	channel := r.PostFormValue("channel_id")
	lastMessage, err := getLastSlackMessage(channel)
	if err != nil {
		status = http.StatusInternalServerError
		return
	}
	params := slack.NewPostMessageParameters()
	params.Username = "Spongebob"
	params.Attachments = []slack.Attachment{{
		Text:     transformText(lastMessage),
		Fallback: "*Spongebob mocking meme*",
		ImageURL: memeURL,
	}}
	params.IconURL = iconURL
	_, _, err = api.PostMessage(channel, "", params)
	if err != nil {
		status = http.StatusInternalServerError
	}
}

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		log.Fatal("$PORT must be set!")
	}

	http.Handle("/static", http.FileServer(http.Dir("/static")))
	http.HandleFunc("/slack", handleSlack)

	log.Fatal(http.ListenAndServe(":"+port, nil))
}
<|endoftext|>


<commit_before>package main

import "fmt"
import "io/ioutil"
import "log"
import "net/url"
import "os"
import "os/exec"
import "path/filepath"
import "strings"

func cleanWcRoot(wcPath string) (err error) {
	infos, err := ioutil.ReadDir(wcPath)
	if nil != err {
		return
	}
	for _, inf := range infos {
		if ".svn" == inf.Name() {
			continue
		}
		fullPath := filepath.Join(wcPath, inf.Name())
		err = os.RemoveAll(fullPath)
		if nil != err {
			return
		}
	}
	return nil
}

func execPiped(name string, arg ...string) error {
	fmt.Println(name + " " + strings.Join(arg, " "))
	cmd := exec.Command(name, arg...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	return cmd.Run()
}

func svnDiffCommit(srcPath string, wcPath string, repos *url.URL) (err error) {
	err = execPiped("svn", "checkout", repos.String(), wcPath)
	if nil != err {
		return
	}
	err = cleanWcRoot(wcPath)
	if nil != err {
		return
	}
	fmt.Println(srcPath) // remove later
	// copy all files and folders from source to working copy
	// svn status
	// svn remove all missing files
	// svn commit
	return nil
}

func createRepos(reposPath string) (repos *url.URL, err error) {
	err = execPiped("svnadmin", "create", reposPath)
	if nil != err {
		return
	}
	absReposPath, err := filepath.Abs(reposPath)
	if nil != err {
		return
	}
	absReposPath = "file://" + absReposPath
	repos, err = url.Parse(absReposPath)
	return
}

struct testData {
	Path string;
	Content string;
	IsDir bool;
}

func makeTestData() []testData {
	var result []testData = {
		{ "1.txt", false, "data1" },
		{ "2.txt", false, "data2" },
		{ "subdir1", true, "" },
		{ filepath.Join("subdir1", "1.txt"), false, "subdata1" },
		{ "subdir2", true, "" }
	}
	return result
}

func createTestSourceFiles(basePath string) (err error) {
	err = os.Mkdir(path, 0755)
	if nil != err {
		return
	}
	testDatas := makeTestData(basePath)
	for _, td := range testDatas {
		if td.IsDir {
			err = os.Mkdir(td.Path)
			if nil != nil {
				return
			}
			continue
		}
		err = ioutil.WriteFile(td.Path, td.Content, 0755)
		if nil != nil {
			return
		}
	}
	return nil
}

func setupTest(testPath string) (repos *url.URL, srcPath string, err error) {
	err = os.Mkdir(testPath, 0755)
	if nil != err {
		return
	}
	srcPath = filepath.Join(testPath, "src")
	err = createTestSourceFiles(srcPath)
	if nil != err {
		return
	}
	reposPath := filepath.Join(testPath, "repos")
	repos, err = createRepos(reposPath)
	return
}

func teardownTest(testPath string) {
	err := os.RemoveAll(testPath)
	if nil != err {
		log.Println("ERROR: ", err)
	}
}

func runSelfTest() (err error) {
	fmt.Print("\n\nSelf test --> Start...\n\n\n")
	testPath := filepath.Join(".", "self_test")
	reposUrl, srcPath, err := setupTest(testPath)
	if nil != err {
		return
	}
	defer teardownTest(testPath)
	wcPath := filepath.Join(testPath, "wc")
	err = svnDiffCommit(srcPath, wcPath, reposUrl)
	if nil != err {
		return
	}
	fmt.Print("\n\nSelf test --> Success.\n\n\n")
	return nil
}

func main() {
	err := runSelfTest()
	if nil != err {
		log.Fatal(err)
	}
}
<commit_msg>Compiles and runs again.<commit_after>package main

import "fmt"
import "io/ioutil"
import "log"
import "net/url"
import "os"
import "os/exec"
import "path/filepath"
import "strings"

func cleanWcRoot(wcPath string) (err error) {
	infos, err := ioutil.ReadDir(wcPath)
	if nil != err {
		return
	}
	for _, inf := range infos {
		if ".svn" == inf.Name() {
			continue
		}
		fullPath := filepath.Join(wcPath, inf.Name())
		err = os.RemoveAll(fullPath)
		if nil != err {
			return
		}
	}
	return nil
}

func execPiped(name string, arg ...string) error {
	fmt.Println(name + " " + strings.Join(arg, " "))
	cmd := exec.Command(name, arg...)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Stdin = os.Stdin
	return cmd.Run()
}

func svnDiffCommit(srcPath string, wcPath string, repos *url.URL) (err error) {
	err = execPiped("svn", "checkout", repos.String(), wcPath)
	if nil != err {
		return
	}
	err = cleanWcRoot(wcPath)
	if nil != err {
		return
	}
	fmt.Println(srcPath) // remove later
	// copy all files and folders from source to working copy
	// svn status
	// svn remove all missing files
	// svn commit
	return nil
}

func createRepos(reposPath string) (repos *url.URL, err error) {
	err = execPiped("svnadmin", "create", reposPath)
	if nil != err {
		return
	}
	absReposPath, err := filepath.Abs(reposPath)
	if nil != err {
		return
	}
	absReposPath = "file://" + absReposPath
	repos, err = url.Parse(absReposPath)
	return
}

type testData struct {
	Path string;
	IsDir bool;
	Content string;
}

func makeTestData() []testData {
	result := []testData {
		{ "1.txt", false, "data1" },
		{ "2.txt", false, "data2" },
		{ "subdir1", true, "" },
		{ filepath.Join("subdir1", "1.txt"), false, "subdata1" },
		{ "subdir2", true, "" } }
	return result
}

const perm = 0755

func createTestSourceFiles(basePath string) (err error) {
	err = os.Mkdir(basePath, perm)
	if nil != err {
		return
	}
	testDatas := makeTestData()
	for _, td := range testDatas {
		path := filepath.Join(basePath, td.Path)
		if td.IsDir {
			err = os.Mkdir(path, perm)
			if nil != err {
				return
			}
			continue
		}
		err = ioutil.WriteFile(path, []byte(td.Content), perm)
		if nil != err {
			return
		}
	}
	return nil
}

func setupTest(testPath string) (repos *url.URL, srcPath string, err error) {
	err = os.Mkdir(testPath, perm)
	if nil != err {
		return
	}
	srcPath = filepath.Join(testPath, "src")
	err = createTestSourceFiles(srcPath)
	if nil != err {
		return
	}
	reposPath := filepath.Join(testPath, "repos")
	repos, err = createRepos(reposPath)
	return
}

func teardownTest(testPath string) {
	err := os.RemoveAll(testPath)
	if nil != err {
		log.Println("ERROR: ", err)
	}
}

func runSelfTest() (err error) {
	fmt.Print("\n\nSelf test --> Start...\n\n\n")
	testPath := filepath.Join(".", "self_test")
	reposUrl, srcPath, err := setupTest(testPath)
	if nil != err {
		return
	}
	defer teardownTest(testPath)
	wcPath := filepath.Join(testPath, "wc")
	err = svnDiffCommit(srcPath, wcPath, reposUrl)
	if nil != err {
		return
	}
	fmt.Print("\n\nSelf test --> Success.\n\n\n")
	return nil
}

func main() {
	err := runSelfTest()
	if nil != err {
		log.Fatal(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"bytes"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"regexp"
	"strings"

	"github.com/phayes/checkstyle"
)

var (
	regexGithub    = regexp.MustCompile("^g")
	regexBitbucket = regexp.MustCompile("^b")
	regexGitURI    = regexp.MustCompile("((git|ssh|http(s)?)|(git@[\\w\\.]+))(:(//)?)([\\w\\.@\\:/\\-~]+)(\\.git)(/)?")
)

var (
	cstyle                                                   = checkstyle.New()
	homeEnv                                                  = os.Getenv("HOME")
	gopathEnv                                                = os.Getenv("GOPATH")
	scrutinizerProjectEnv                                    = os.Getenv("SCRUTINIZER_PROJECT")
	projectFull, projectDomain, projectOwner, projectProject string
)

func main() {
	// Set up environment variables
	if gopathEnv == "" {
		gopathEnv = homeEnv + "/go"
	}

	// Set up project
	if len(scrutinizerProjectEnv) == 0 {
		log.Fatal("Not running without scrutinizer environemnt. SCRUTINIZER_PROJECT environment variable not found")
	}

	projectFull = regexGithub.ReplaceAllString(scrutinizerProjectEnv, "github.com")
	projectFull = regexBitbucket.ReplaceAllString(projectFull, "bitbucket.com")

	projectParts := strings.Split(projectFull, "/")
	if len(projectParts) != 3 {
		log.Fatal("Malformed SCRUTINIZER_PROJECT environment variable.")
	}
	projectDomain = projectParts[0]
	projectOwner = projectParts[1]
	projectProject = projectParts[2]

	// Install all dependancies
	cmd := exec.Command("go", "get", "-t", "./...")
	_, err := cmd.Output()
	if err != nil {
		exitErr, ok := err.(*exec.ExitError)
		if ok && len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go get -t ./...", err)
	}

	// Run metalinter
	metalinter()

	// Run tests and coverage
	testAndCoverage()
}

func metalinter() {
	goMetaLinterCmd := gopathEnv + "/bin/gometalinter"

	// Install gometalinter -- no-op if already installed
	cmd := exec.Command("go", "get", "github.com/alecthomas/gometalinter")
	_, err := cmd.Output()
	if err != nil {
		exitErr, ok := err.(*exec.ExitError)
		if ok && len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go get github.com/alecthomas/gometalinter", err)
	}

	// Install all gometalinter dependancies -- no-op if already installed
	cmd = exec.Command(goMetaLinterCmd, "--install")
	_, err = cmd.Output()
	if err != nil {
		exitErr, ok := err.(*exec.ExitError)
		if ok && len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal(goMetaLinterCmd, "--install", err)
	}

	// Configure the metalinter
	if _, err := os.Stat("go-scrutinize.config"); os.IsNotExist(err) {
		cmd = exec.Command(goMetaLinterCmd, "--checkstyle")
	} else {
		cmd = exec.Command(goMetaLinterCmd, "--checkstyle", "--config=go-scrutinize.config")
	}

	// Run the metalinter
	out, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Println(string(out))
		log.Fatal(goMetaLinterCmd, err)
	}

	// Write the output from the metalinter
	ioutil.WriteFile("checkstyle_report.xml", out, os.ModePerm)
}

func testAndCoverage() {
	goConvCmd := gopathEnv + "/bin/gocov"
	goConvXMLCmd := gopathEnv + "/bin/gocov-xml"

	// Install the coverage tool covov
	cmd := exec.Command("go", "get", "github.com/axw/gocov/...")
	_, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go", "get", "github.com/axw/gocov/...", err)
	}

	// Install the coverage file translation tool gocov-xml
	cmd = exec.Command("go", "get", "github.com/AlekSi/gocov-xml")
	_, err = cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go", "get", "github.com/AlekSi/gocov-xml", err)
	}

	// Run tests with coverage
	cmd = exec.Command(goConvCmd, "test", "./...", "-race")
	gocovout, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal(goConvCmd, "test", "./...", "-race", err)
	}

	// Convert to clover format
	cmd = exec.Command(goConvXMLCmd)
	cmd.Stdin = bytes.NewReader(gocovout)
	xmlout, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal(goConvXMLCmd, err)
	}

	// Rewrite all filenames to use /home/scrutinizer/build paths
	coveragexml := strings.Replace(string(xmlout), gopathEnv+"/src/"+projectFull, "/home/scrutinizer/build", 0)

	// Write the output from the metalinter
	ioutil.WriteFile("coverage.xml", []byte(coveragexml), os.ModePerm)

}
<commit_msg>Fixing metalinter command - non-zero exit status OK<commit_after>package main

import (
	"bytes"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"regexp"
	"strings"

	"github.com/phayes/checkstyle"
)

var (
	regexGithub    = regexp.MustCompile("^g")
	regexBitbucket = regexp.MustCompile("^b")
	regexGitURI    = regexp.MustCompile("((git|ssh|http(s)?)|(git@[\\w\\.]+))(:(//)?)([\\w\\.@\\:/\\-~]+)(\\.git)(/)?")
)

var (
	cstyle                                                   = checkstyle.New()
	homeEnv                                                  = os.Getenv("HOME")
	gopathEnv                                                = os.Getenv("GOPATH")
	scrutinizerProjectEnv                                    = os.Getenv("SCRUTINIZER_PROJECT")
	projectFull, projectDomain, projectOwner, projectProject string
)

func main() {
	// Set up environment variables
	if gopathEnv == "" {
		gopathEnv = homeEnv + "/go"
	}

	// Set up project
	if len(scrutinizerProjectEnv) == 0 {
		log.Fatal("Not running without scrutinizer environemnt. SCRUTINIZER_PROJECT environment variable not found")
	}

	projectFull = regexGithub.ReplaceAllString(scrutinizerProjectEnv, "github.com")
	projectFull = regexBitbucket.ReplaceAllString(projectFull, "bitbucket.com")

	projectParts := strings.Split(projectFull, "/")
	if len(projectParts) != 3 {
		log.Fatal("Malformed SCRUTINIZER_PROJECT environment variable.")
	}
	projectDomain = projectParts[0]
	projectOwner = projectParts[1]
	projectProject = projectParts[2]

	// Install all dependancies
	cmd := exec.Command("go", "get", "-t", "./...")
	_, err := cmd.Output()
	if err != nil {
		exitErr, ok := err.(*exec.ExitError)
		if ok && len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go get -t ./...", err)
	}

	// Run metalinter
	metalinter()

	// Run tests and coverage
	testAndCoverage()
}

func metalinter() {
	goMetaLinterCmd := gopathEnv + "/bin/gometalinter"

	// Install gometalinter -- no-op if already installed
	cmd := exec.Command("go", "get", "github.com/alecthomas/gometalinter")
	_, err := cmd.Output()
	if err != nil {
		exitErr, ok := err.(*exec.ExitError)
		if ok && len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go get github.com/alecthomas/gometalinter", err)
	}

	// Install all gometalinter dependancies -- no-op if already installed
	cmd = exec.Command(goMetaLinterCmd, "--install")
	_, err = cmd.Output()
	if err != nil {
		exitErr, ok := err.(*exec.ExitError)
		if ok && len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal(goMetaLinterCmd, "--install", err)
	}

	// Configure the metalinter
	if _, err := os.Stat("go-scrutinize.config"); os.IsNotExist(err) {
		cmd = exec.Command(goMetaLinterCmd, "--checkstyle")
	} else {
		cmd = exec.Command(goMetaLinterCmd, "--checkstyle", "--config=go-scrutinize.config")
	}

	// Run the metalinter -- note that will return non-zero exit status
	out, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
	}

	// Write the output from the metalinter
	ioutil.WriteFile("checkstyle_report.xml", out, os.ModePerm)
}

func testAndCoverage() {
	goConvCmd := gopathEnv + "/bin/gocov"
	goConvXMLCmd := gopathEnv + "/bin/gocov-xml"

	// Install the coverage tool covov
	cmd := exec.Command("go", "get", "github.com/axw/gocov/...")
	_, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go", "get", "github.com/axw/gocov/...", err)
	}

	// Install the coverage file translation tool gocov-xml
	cmd = exec.Command("go", "get", "github.com/AlekSi/gocov-xml")
	_, err = cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal("go", "get", "github.com/AlekSi/gocov-xml", err)
	}

	// Run tests with coverage
	cmd = exec.Command(goConvCmd, "test", "./...", "-race")
	gocovout, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal(goConvCmd, "test", "./...", "-race", err)
	}

	// Convert to clover format
	cmd = exec.Command(goConvXMLCmd)
	cmd.Stdin = bytes.NewReader(gocovout)
	xmlout, err := cmd.Output()
	if err != nil {
		exitErr := err.(*exec.ExitError)
		if len(exitErr.Stderr) != 0 {
			log.Println(string(exitErr.Stderr))
		}
		log.Fatal(goConvXMLCmd, err)
	}

	// Rewrite all filenames to use /home/scrutinizer/build paths
	coveragexml := strings.Replace(string(xmlout), gopathEnv+"/src/"+projectFull, "/home/scrutinizer/build", 0)

	// Write the output from the metalinter
	ioutil.WriteFile("coverage.xml", []byte(coveragexml), os.ModePerm)

}
<|endoftext|>


<commit_before>package main

import (
	"errors"
	"fmt"
	"github.com/jackc/cli"
	"github.com/jackc/pgx"
	"github.com/jackc/tern/migrate"
	"github.com/vaughan0/go-ini"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

const VERSION = "1.3.0"

var defaultConf = `[database]
# host is required (network host or path to Unix domain socket)
# host =
# port = 5432
# database is required
# database =
# user defaults to OS user
# user =
# version_table = schema_version

[data]
# Any fields in the data section are available in migration templates
# prefix = foo
`

var sampleMigration = `-- This is a sample migration.

create table people(
  id serial primary key,
  first_name varchar not null,
  last_name varchar not null
);

---- create above / drop below ----

drop table people;
`

var newMigrationText = `-- Write your migrate up statements here

---- create above / drop below ----

-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.
`

type Config struct {
	ConnConfig   pgx.ConnConfig
	VersionTable string
	Data         map[string]interface{}
}

func (c *Config) Validate() error {
	if c.ConnConfig.Host == "" {
		return errors.New("Config must contain host but it does not")
	}

	if c.ConnConfig.Database == "" {
		return errors.New("Config must contain database but it does not")
	}

	return nil
}

func main() {
	app := cli.NewApp()
	app.Name = "tern"
	app.Usage = "PostgreSQL database migrator"
	app.Version = VERSION
	app.Author = "Jack Christensen"
	app.Email = "jack@jackchristensen.com"

	app.Commands = []cli.Command{
		{
			Name:        "init",
			ShortName:   "i",
			Usage:       "init a new tern project",
			Synopsis:    "[directory]",
			Description: "Initialize a new tern project in directory",
			Action:      Init,
		},
		{
			Name:        "migrate",
			ShortName:   "m",
			Usage:       "migrate the database",
			Synopsis:    "[command options]",
			Description: "migrate the database to destination version",
			Flags: []cli.Flag{
				cli.StringFlag{"destination, d", "last", "Destination migration version"},
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
				cli.StringFlag{"config, c", "tern.conf", "Config path"},
			},
			Action: Migrate,
		},
		{
			Name:        "new",
			ShortName:   "n",
			Usage:       "generate a new migration",
			Synopsis:    "[command options] name",
			Description: "generate a new migration with the next sequence number and provided name",
			Flags: []cli.Flag{
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
			},
			Action: NewMigration,
		},
	}

	app.Run(os.Args)
}

func Init(c *cli.Context) {
	var directory string
	switch len(c.Args()) {
	case 0:
		directory = "."
	case 1:
		directory = c.Args()[0]
		err := os.Mkdir(directory, os.ModePerm)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	default:
		cli.ShowCommandHelp(c, c.Command.Name)
		os.Exit(1)
	}

	// Write default conf file
	confPath := filepath.Join(directory, "tern.conf")
	confFile, err := os.OpenFile(confPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer confFile.Close()

	_, err = confFile.WriteString(defaultConf)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	// Write sample migration
	smPath := filepath.Join(directory, "001_create_people.sql.example")
	smFile, err := os.OpenFile(smPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer smFile.Close()

	_, err = smFile.WriteString(sampleMigration)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func NewMigration(c *cli.Context) {
	if len(c.Args()) != 1 {
		cli.ShowCommandHelp(c, c.Command.Name)
		os.Exit(1)
	}

	name := c.Args()[0]

	migrationsPath := c.String("migrations")
	migrations, err := migrate.FindMigrations(migrationsPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading migrations:\n  %v\n", err)
		os.Exit(1)
	}

	newMigrationName := fmt.Sprintf("%03d_%s.sql", len(migrations)+1, name)

	// Write new migration
	mPath := filepath.Join(migrationsPath, newMigrationName)
	mFile, err := os.OpenFile(mPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer mFile.Close()

	_, err = mFile.WriteString(newMigrationText)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

}

func Migrate(c *cli.Context) {
	config, err := ReadConfig(c.String("config"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error reading config:\n  %v\n", err)
		os.Exit(1)
	}

	err = config.Validate()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Invalid config:\n  %v\n", err)
		os.Exit(1)
	}

	var conn *pgx.Conn
	conn, err = pgx.Connect(config.ConnConfig)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to PostgreSQL:\n  %v\n", err)
		os.Exit(1)
	}
	defer conn.Close()

	migrator, err := migrate.NewMigrator(conn, config.VersionTable)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error initializing migrator:\n  %v\n", err)
		os.Exit(1)
	}
	migrator.Data = config.Data

	migrationsPath := c.String("migrations")
	err = migrator.LoadMigrations(migrationsPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading migrations:\n  %v\n", err)
		os.Exit(1)
	}
	if len(migrator.Migrations) == 0 {
		fmt.Fprintln(os.Stderr, "No migrations found")
		os.Exit(1)
	}

	migrator.OnStart = func(sequence int32, name, direction, sql string) {
		fmt.Printf("%s executing %s %s\n%s\n\n", time.Now().Format("2006-01-02 15:04:05"), name, direction, sql)
	}

	destination := c.String("destination")
	if destination == "last" {
		err = migrator.Migrate()
	} else {
		var n int64
		n, err = strconv.ParseInt(destination, 10, 32)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Bad destination:\n  %v\n", err)
			os.Exit(1)
		}

		err = migrator.MigrateTo(int32(n))
	}

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error migrating:\n  %v\n", err)
		os.Exit(1)
	}
}

func ReadConfig(path string) (*Config, error) {
	file, err := ini.LoadFile(path)
	if err != nil {
		return nil, err
	}

	config := &Config{VersionTable: "schema_version"}

	config.ConnConfig.Host, _ = file.Get("database", "host")

	// For backwards compatibility if host isn't set look for socket.
	if config.ConnConfig.Host == "" {
		config.ConnConfig.Host, _ = file.Get("database", "socket")
	}

	if p, ok := file.Get("database", "port"); ok {
		n, err := strconv.ParseUint(p, 10, 16)
		config.ConnConfig.Port = uint16(n)
		if err != nil {
			return nil, err
		}
	}

	config.ConnConfig.Database, _ = file.Get("database", "database")
	config.ConnConfig.User, _ = file.Get("database", "user")
	config.ConnConfig.Password, _ = file.Get("database", "password")

	if vt, ok := file.Get("database", "version_table"); ok {
		config.VersionTable = vt
	}

	config.Data = make(map[string]interface{})
	for key, value := range file["data"] {
		config.Data[key] = value
	}

	return config, nil
}
<commit_msg>Release 1.3.1<commit_after>package main

import (
	"errors"
	"fmt"
	"github.com/jackc/cli"
	"github.com/jackc/pgx"
	"github.com/jackc/tern/migrate"
	"github.com/vaughan0/go-ini"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

const VERSION = "1.3.1"

var defaultConf = `[database]
# host is required (network host or path to Unix domain socket)
# host =
# port = 5432
# database is required
# database =
# user defaults to OS user
# user =
# version_table = schema_version

[data]
# Any fields in the data section are available in migration templates
# prefix = foo
`

var sampleMigration = `-- This is a sample migration.

create table people(
  id serial primary key,
  first_name varchar not null,
  last_name varchar not null
);

---- create above / drop below ----

drop table people;
`

var newMigrationText = `-- Write your migrate up statements here

---- create above / drop below ----

-- Write your migrate down statements here. If this migration is irreversible
-- Then delete the separator line above.
`

type Config struct {
	ConnConfig   pgx.ConnConfig
	VersionTable string
	Data         map[string]interface{}
}

func (c *Config) Validate() error {
	if c.ConnConfig.Host == "" {
		return errors.New("Config must contain host but it does not")
	}

	if c.ConnConfig.Database == "" {
		return errors.New("Config must contain database but it does not")
	}

	return nil
}

func main() {
	app := cli.NewApp()
	app.Name = "tern"
	app.Usage = "PostgreSQL database migrator"
	app.Version = VERSION
	app.Author = "Jack Christensen"
	app.Email = "jack@jackchristensen.com"

	app.Commands = []cli.Command{
		{
			Name:        "init",
			ShortName:   "i",
			Usage:       "init a new tern project",
			Synopsis:    "[directory]",
			Description: "Initialize a new tern project in directory",
			Action:      Init,
		},
		{
			Name:        "migrate",
			ShortName:   "m",
			Usage:       "migrate the database",
			Synopsis:    "[command options]",
			Description: "migrate the database to destination version",
			Flags: []cli.Flag{
				cli.StringFlag{"destination, d", "last", "Destination migration version"},
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
				cli.StringFlag{"config, c", "tern.conf", "Config path"},
			},
			Action: Migrate,
		},
		{
			Name:        "new",
			ShortName:   "n",
			Usage:       "generate a new migration",
			Synopsis:    "[command options] name",
			Description: "generate a new migration with the next sequence number and provided name",
			Flags: []cli.Flag{
				cli.StringFlag{"migrations, m", ".", "Migrations path"},
			},
			Action: NewMigration,
		},
	}

	app.Run(os.Args)
}

func Init(c *cli.Context) {
	var directory string
	switch len(c.Args()) {
	case 0:
		directory = "."
	case 1:
		directory = c.Args()[0]
		err := os.Mkdir(directory, os.ModePerm)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	default:
		cli.ShowCommandHelp(c, c.Command.Name)
		os.Exit(1)
	}

	// Write default conf file
	confPath := filepath.Join(directory, "tern.conf")
	confFile, err := os.OpenFile(confPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer confFile.Close()

	_, err = confFile.WriteString(defaultConf)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	// Write sample migration
	smPath := filepath.Join(directory, "001_create_people.sql.example")
	smFile, err := os.OpenFile(smPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer smFile.Close()

	_, err = smFile.WriteString(sampleMigration)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func NewMigration(c *cli.Context) {
	if len(c.Args()) != 1 {
		cli.ShowCommandHelp(c, c.Command.Name)
		os.Exit(1)
	}

	name := c.Args()[0]

	migrationsPath := c.String("migrations")
	migrations, err := migrate.FindMigrations(migrationsPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading migrations:\n  %v\n", err)
		os.Exit(1)
	}

	newMigrationName := fmt.Sprintf("%03d_%s.sql", len(migrations)+1, name)

	// Write new migration
	mPath := filepath.Join(migrationsPath, newMigrationName)
	mFile, err := os.OpenFile(mPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, os.ModePerm)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	defer mFile.Close()

	_, err = mFile.WriteString(newMigrationText)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

}

func Migrate(c *cli.Context) {
	config, err := ReadConfig(c.String("config"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error reading config:\n  %v\n", err)
		os.Exit(1)
	}

	err = config.Validate()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Invalid config:\n  %v\n", err)
		os.Exit(1)
	}

	var conn *pgx.Conn
	conn, err = pgx.Connect(config.ConnConfig)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to connect to PostgreSQL:\n  %v\n", err)
		os.Exit(1)
	}
	defer conn.Close()

	migrator, err := migrate.NewMigrator(conn, config.VersionTable)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error initializing migrator:\n  %v\n", err)
		os.Exit(1)
	}
	migrator.Data = config.Data

	migrationsPath := c.String("migrations")
	err = migrator.LoadMigrations(migrationsPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading migrations:\n  %v\n", err)
		os.Exit(1)
	}
	if len(migrator.Migrations) == 0 {
		fmt.Fprintln(os.Stderr, "No migrations found")
		os.Exit(1)
	}

	migrator.OnStart = func(sequence int32, name, direction, sql string) {
		fmt.Printf("%s executing %s %s\n%s\n\n", time.Now().Format("2006-01-02 15:04:05"), name, direction, sql)
	}

	destination := c.String("destination")
	if destination == "last" {
		err = migrator.Migrate()
	} else {
		var n int64
		n, err = strconv.ParseInt(destination, 10, 32)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Bad destination:\n  %v\n", err)
			os.Exit(1)
		}

		err = migrator.MigrateTo(int32(n))
	}

	if err != nil {
		fmt.Fprintf(os.Stderr, "Error migrating:\n  %v\n", err)
		os.Exit(1)
	}
}

func ReadConfig(path string) (*Config, error) {
	file, err := ini.LoadFile(path)
	if err != nil {
		return nil, err
	}

	config := &Config{VersionTable: "schema_version"}

	config.ConnConfig.Host, _ = file.Get("database", "host")

	// For backwards compatibility if host isn't set look for socket.
	if config.ConnConfig.Host == "" {
		config.ConnConfig.Host, _ = file.Get("database", "socket")
	}

	if p, ok := file.Get("database", "port"); ok {
		n, err := strconv.ParseUint(p, 10, 16)
		config.ConnConfig.Port = uint16(n)
		if err != nil {
			return nil, err
		}
	}

	config.ConnConfig.Database, _ = file.Get("database", "database")
	config.ConnConfig.User, _ = file.Get("database", "user")
	config.ConnConfig.Password, _ = file.Get("database", "password")

	if vt, ok := file.Get("database", "version_table"); ok {
		config.VersionTable = vt
	}

	config.Data = make(map[string]interface{})
	for key, value := range file["data"] {
		config.Data[key] = value
	}

	return config, nil
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"os"
	"runtime"
	"strings"
	"sync"
	"time"
)

var (
	flagDividendStart        = flag.Int("dividendstart", 0, "Start of range of dividends")
	flagDividendEnd          = flag.Int("dividendend", 0xffff, "End of range of dividends")
	flagDivisorStart         = flag.Int("divisorstart", 2, "Start of range of divisors")
	flagDivisorEnd           = flag.Int("divisorend", 1000, "End of range of divisors")
	flagMaxProcs             = flag.Int("maxprocs", 0, "Value for GOMAXPROCS (value may be reduced, respecting default GOMAXPROCS and number of CPUs)")
	flagOutputAllDivisors    = flag.Bool("outputalldivisors", false, "Output result for all divisors")
	flagOutputAllDifferences = flag.Bool("outputalldifferences", false, "Output every dividend / divisor combination which provides a flase result")
	flagOutputAll            = flag.Bool("outputall", false, "Output every calculation, includes -outputalldivisors and -outputalldifferences; WARNING: very verbose, use only with small ranges for dividend and divisor, maxprocs should be set to 1")
	flagIgnorePow2           = flag.Bool("ignorepow2", true, "for power of 2 divisors the solution is trivial, therefore these divisors may be ignored")
	flagUsePow2              = flag.Bool("usepow2", true, "use bitwise and for power of 2 divisors")
	flagHelp                 = flag.Bool("help", false, "Print help")
	flagNumType              = flag.String("numtype", "uint32", "numeric type, used for the calculations (one of: uint8, uint16, uint32, uint64)")
)

func main() {
	flag.Parse()

	if *flagHelp {
		flag.Usage()
		os.Exit(0)
	}

	numType := strings.ToLower(*flagNumType)
	switch numType {
	case "uint8", "uint16", "uint32", "uint64":
	default:
		fmt.Println("numtype must be one of: uint8, uint16, uint32, uint64")
		os.Exit(1)
	}

	runtime.GOMAXPROCS(MaxParallelism(*flagMaxProcs))

	var wg sync.WaitGroup

	var minDividend uint64 = uint64(*flagDividendStart)
	var maxDividend uint64 = uint64(*flagDividendEnd) // fully tested for 0xffffff, may take a long time

	var minDivisor uint64 = uint64(*flagDivisorStart) // min is 2 because negative number and zero don't make sense, and 1 means every packet (no selection)
	var maxDivisor uint64 = uint64(*flagDivisorEnd)   // tested until 100000

	if *flagOutputAll {
		flag.Set("outputalldivisors", "true")
		flag.Set("outputalldifferences", "true")
	}

	var aDivisor uint64
	for aDivisor = minDivisor; aDivisor <= maxDivisor; aDivisor++ {
		go func(divisor uint64) {
			wg.Add(1)
			defer wg.Done()

			var dividend uint64
			var d Dividable

			// Check if divisor is a power of 2
			if divisor&(divisor-1) == 0 {
				if *flagIgnorePow2 {
					return
				}
				if *flagUsePow2 {
					d = NewDivisionPow2(divisor)
				}
			}

			if d == nil {
				switch numType {
				case "uint8":
					d = NewZeroremainderUint8(divisor)
				case "uint16":
					d = NewZeroremainderUint16(divisor)
				case "uint32":
					d = NewZeroremainderUint32(divisor)
				case "uint64":
					d = NewZeroremainderUint64(divisor)
				}
			}

			var zeroremainderCount, exactCount, zeroremainderWrong int
			for dividend = minDividend; dividend <= maxDividend; dividend++ {
				zeroremainder := d.IsRestlessDividable(dividend)
				exact := (dividend%divisor == 0)
				if *flagOutputAll {
					fmt.Println("CALC Dividend:", dividend, "Divisor:", divisor, "Zeroremainder:", d.IsRestlessDividable(dividend), "Exact:", (dividend%divisor == 0), "Correct:", zeroremainder == exact)
				}
				if zeroremainder != exact {
					zeroremainderWrong++
					if *flagOutputAllDifferences {
						fmt.Println("DIFF Dividend:", dividend, "Divisor:", divisor, "Zeroremainder:", d.IsRestlessDividable(dividend), "Exact:", (dividend%divisor == 0), "Reciprocal:", d.GetReciprocal())
					}
				}
				zeroremainderCount += Btoi(zeroremainder)
				exactCount += Btoi(exact)
			}
			if Round(1.0/float64(exactCount)*float64(zeroremainderCount), 4) != 1.0 || *flagOutputAllDivisors {
				fmt.Println("SUMMARY Divisor:", divisor, "Zeroremainder counts:", zeroremainderCount, "Modulo counts:", exactCount, "Difference (%):", 100.0-Round(100.0/float64(exactCount)*float64(zeroremainderCount), 4), "Differences between Zeroremainder and exact:", zeroremainderWrong, "Reciprocal:", d.GetReciprocal())
			}
		}(aDivisor)
	}

	time.Sleep(1 * time.Second)
	wg.Wait()
}
<commit_msg>Changed flag outputall to outputallcalc<commit_after>package main

import (
	"flag"
	"fmt"
	"os"
	"runtime"
	"strings"
	"sync"
	"time"
)

var (
	flagDividendStart        = flag.Int("dividendstart", 0, "Start of range of dividends")
	flagDividendEnd          = flag.Int("dividendend", 0xffff, "End of range of dividends")
	flagDivisorStart         = flag.Int("divisorstart", 2, "Start of range of divisors")
	flagDivisorEnd           = flag.Int("divisorend", 1000, "End of range of divisors")
	flagMaxProcs             = flag.Int("maxprocs", 0, "Value for GOMAXPROCS (value may be reduced, respecting default GOMAXPROCS and number of CPUs)")
	flagOutputAllDivisors    = flag.Bool("outputalldivisors", false, "Output result for all divisors")
	flagOutputAllDifferences = flag.Bool("outputalldifferences", false, "Output every dividend / divisor combination which provides a flase result")
	flagOutputAllCalc        = flag.Bool("outputallcalc", false, "Output every calculation; WARNING: very verbose, use only with small ranges for dividend and divisor, maxprocs should be set to 1")
	flagIgnorePow2           = flag.Bool("ignorepow2", true, "for power of 2 divisors the solution is trivial, therefore these divisors may be ignored")
	flagUsePow2              = flag.Bool("usepow2", true, "use bitwise and for power of 2 divisors")
	flagHelp                 = flag.Bool("help", false, "Print help")
	flagNumType              = flag.String("numtype", "uint32", "numeric type, used for the calculations (one of: uint8, uint16, uint32, uint64)")
)

func main() {
	flag.Parse()

	if *flagHelp {
		flag.Usage()
		os.Exit(0)
	}

	numType := strings.ToLower(*flagNumType)
	switch numType {
	case "uint8", "uint16", "uint32", "uint64":
	default:
		fmt.Println("numtype must be one of: uint8, uint16, uint32, uint64")
		os.Exit(1)
	}

	runtime.GOMAXPROCS(MaxParallelism(*flagMaxProcs))

	var wg sync.WaitGroup

	var minDividend uint64 = uint64(*flagDividendStart)
	var maxDividend uint64 = uint64(*flagDividendEnd) // fully tested for 0xffffff, may take a long time

	var minDivisor uint64 = uint64(*flagDivisorStart) // min is 2 because negative number and zero don't make sense, and 1 means every packet (no selection)
	var maxDivisor uint64 = uint64(*flagDivisorEnd)   // tested until 100000

	var aDivisor uint64
	for aDivisor = minDivisor; aDivisor <= maxDivisor; aDivisor++ {
		go func(divisor uint64) {
			wg.Add(1)
			defer wg.Done()

			var dividend uint64
			var d Dividable

			// Check if divisor is a power of 2
			if divisor&(divisor-1) == 0 {
				if *flagIgnorePow2 {
					return
				}
				if *flagUsePow2 {
					d = NewDivisionPow2(divisor)
				}
			}

			if d == nil {
				switch numType {
				case "uint8":
					d = NewZeroremainderUint8(divisor)
				case "uint16":
					d = NewZeroremainderUint16(divisor)
				case "uint32":
					d = NewZeroremainderUint32(divisor)
				case "uint64":
					d = NewZeroremainderUint64(divisor)
				}
			}

			var zeroremainderCount, exactCount, zeroremainderWrong int
			for dividend = minDividend; dividend <= maxDividend; dividend++ {
				zeroremainder := d.IsRestlessDividable(dividend)
				exact := (dividend%divisor == 0)
				if *flagOutputAllCalc {
					fmt.Println("CALC Dividend:", dividend, "Divisor:", divisor, "Zeroremainder:", d.IsRestlessDividable(dividend), "Exact:", (dividend%divisor == 0), "Correct:", zeroremainder == exact)
				}
				if zeroremainder != exact {
					zeroremainderWrong++
					if *flagOutputAllDifferences {
						fmt.Println("DIFF Dividend:", dividend, "Divisor:", divisor, "Zeroremainder:", d.IsRestlessDividable(dividend), "Exact:", (dividend%divisor == 0), "Reciprocal:", d.GetReciprocal())
					}
				}
				zeroremainderCount += Btoi(zeroremainder)
				exactCount += Btoi(exact)
			}
			if Round(1.0/float64(exactCount)*float64(zeroremainderCount), 4) != 1.0 || *flagOutputAllDivisors {
				fmt.Println("SUMMARY Divisor:", divisor, "Zeroremainder counts:", zeroremainderCount, "Modulo counts:", exactCount, "Difference (%):", 100.0-Round(100.0/float64(exactCount)*float64(zeroremainderCount), 4), "Differences between Zeroremainder and exact:", zeroremainderWrong, "Reciprocal:", d.GetReciprocal())
			}
		}(aDivisor)
	}

	time.Sleep(1 * time.Second)
	wg.Wait()
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/drone-plugins/drone-git-push/repo"
	"github.com/drone/drone-go/drone"
	"github.com/drone/drone-go/plugin"
)

var (
	build          string
	buildDate      string
	privateKeyPath string = "/root/.ssh/id_rsa"
)

type DeployWorkspace struct {
	Workspace drone.Workspace
}

func main() {
	fmt.Printf("Drone Capistrano Plugin built at %s\n", buildDate)

	workspace := drone.Workspace{}
	vargs := Params{}

	dw := DeployWorkspace{workspace}

	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &vargs)
	plugin.MustParse()

	log("Installing Drone's ssh key")
	if err := repo.WriteKey(&workspace); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	os.Setenv("BUILD_PATH", workspace.Path)
	os.Setenv("GIT_SSH_KEY", privateKeyPath)

	tasks := strings.Fields(vargs.Tasks)

	if len(tasks) == 0 {
		fmt.Println("Please provide Capistrano tasks to execute")
		os.Exit(1)
		return
	}

	log("Running Bundler")
	bundle := dw.bundle(bundlerArgs(vargs)...)
	if err := bundle.Run(); err != nil {
		fmt.Println(err)
		os.Exit(1)
		return
	}

	log("Running Capistrano")
	capistrano := dw.cap(tasks...)
	if err := capistrano.Run(); err != nil {
		fmt.Println(err)
		os.Exit(1)
		return
	}
}

func bundlerArgs(vargs Params) []string {
	args := []string{"install"}
	if ! vargs.Debug {
		args = append(args, "--quiet")
	}
	if len(vargs.BundlePath) > 0 {
		args = append(args, "--path", vargs.BundlePath)
	}
	return args
}

func (w *DeployWorkspace) cap(tasks ...string) *exec.Cmd {
	args := append([]string{"exec", "cap"}, tasks...)
	return w.bundle(args...)
}

func (w *DeployWorkspace) bundle(args ...string) *exec.Cmd {
	return w.command("/bundle.sh", args...)
}

func (w *DeployWorkspace) command(cmd string, args ...string) *exec.Cmd {
	c := exec.Command(cmd, args...)
	c.Dir = w.Workspace.Path
	c.Env = os.Environ()
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	return c
}

func log(message string, a ...interface{}) {
	fmt.Printf("=> %s\n", fmt.Sprintf(message, a...))
}
<commit_msg>printing BUNDLE_APP_CONFIG in debug mode<commit_after>package main

import (
	"fmt"
	"os"
	"os/exec"
	"strings"

	"github.com/drone-plugins/drone-git-push/repo"
	"github.com/drone/drone-go/drone"
	"github.com/drone/drone-go/plugin"
)

var (
	build          string
	buildDate      string
	privateKeyPath string = "/root/.ssh/id_rsa"
)

type DeployWorkspace struct {
	Workspace drone.Workspace
}

func main() {
	fmt.Printf("Drone Capistrano Plugin built at %s\n", buildDate)

	workspace := drone.Workspace{}
	vargs := Params{}

	dw := DeployWorkspace{workspace}

	plugin.Param("workspace", &workspace)
	plugin.Param("vargs", &vargs)
	plugin.MustParse()

	log("Installing Drone's ssh key")
	if err := repo.WriteKey(&workspace); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	os.Setenv("BUILD_PATH", workspace.Path)
	os.Setenv("GIT_SSH_KEY", privateKeyPath)

	if vargs.Debug {
		bundleAppConfig := os.Getenv("BUNDLE_APP_CONFIG")
		fmt.Printf("BUNDLE_APP_CONFIG: %s\n", bundleAppConfig)
	}

	tasks := strings.Fields(vargs.Tasks)

	if len(tasks) == 0 {
		fmt.Println("Please provide Capistrano tasks to execute")
		os.Exit(1)
		return
	}

	log("Running Bundler")
	bundle := dw.bundle(bundlerArgs(vargs)...)
	if err := bundle.Run(); err != nil {
		fmt.Println(err)
		os.Exit(1)
		return
	}

	log("Running Capistrano")
	capistrano := dw.cap(tasks...)
	if err := capistrano.Run(); err != nil {
		fmt.Println(err)
		os.Exit(1)
		return
	}
}

func bundlerArgs(vargs Params) []string {
	args := []string{"install"}
	if ! vargs.Debug {
		args = append(args, "--quiet")
	}
	if len(vargs.BundlePath) > 0 {
		args = append(args, "--path", vargs.BundlePath)
	}
	return args
}

func (w *DeployWorkspace) cap(tasks ...string) *exec.Cmd {
	args := append([]string{"exec", "cap"}, tasks...)
	return w.bundle(args...)
}

func (w *DeployWorkspace) bundle(args ...string) *exec.Cmd {
	return w.command("/bundle.sh", args...)
}

func (w *DeployWorkspace) command(cmd string, args ...string) *exec.Cmd {
	c := exec.Command(cmd, args...)
	c.Dir = w.Workspace.Path
	c.Env = os.Environ()
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	return c
}

func log(message string, a ...interface{}) {
	fmt.Printf("=> %s\n", fmt.Sprintf(message, a...))
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"os/user"
	"strconv"
	"strings"
	"time"
)

var (
	device    string
	message   string
	priority  string
	sound     string
	timestamp string
	title     string
	token_key string
	url_path  string
	url_title string
	user_key  string
)

var verbose bool
var version bool

// Semantic versioning - http://semver.org/
const (
	Major = 1
	Minor = 0
	Patch = 0
)

// init process command line flags and read in user created api keys file.
func init() {
	flag.BoolVar(&version, "version", false, "Display program version.")

	flag.BoolVar(&verbose, "v", false, "Display message response details.")

	flag.StringVar(&device, "device", "", "Send message directly to this "+
		"device, rather than all devices.")

	flag.StringVar(&message, "msg", "", "(Required) - Your message.")

	flag.StringVar(&priority, "priority", "", "Message priority. -2 = lowest "+
		", -1 = low, 0 = normal, 1 = high, 2 = emergency.")

	flag.StringVar(&sound, "sound", "", "Sound to play when user receives "+
		"notification, overrides the user's default sound choice.")

	flag.StringVar(&timestamp, "timestamp", "", "A Unix timestamp of your "+
		"message's date and time to display to the user.")

	flag.StringVar(&title, "title", "", "Your message's title, otherwise "+
		"your app's name is used.")

	flag.StringVar(&url_path, "url", "", "A supplementary URL to show with "+
		"your message.")

	flag.StringVar(&url_title, "url-title", "", "A title for your "+
		"supplementary URL, otherwise just the URL is shown.")

	flag.Parse()

	if version {
		fmt.Println("Pushover", fmt.Sprintf("%d.%d.%d", Major, Minor, Patch))
		os.Exit(0)
	}

	if message == "" {
		fmt.Println("-msg is a required flag. You must specify a message, " +
			"use -h for help.")
		os.Exit(1)
	}

	u, err := user.Current()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	file, err := os.Open(u.HomeDir + "/.pushover")
	if err != nil {
		fmt.Println(err)
		fmt.Println("You need to create a dot file '.pushover' in your home " +
			"directory, containing your token and user api keys, in the " +
			"format:\ntoken=XXXXXXXXXX\nuser=XXXXXXXXXX")
		os.Exit(1)
	}

	// read in user created api keys file.
	r := bufio.NewReaderSize(file, 50)
	for l, err := r.ReadString('\n'); err == nil; l, err = r.ReadString('\n') {
		t := strings.Split(l, "=")
		if t[0] == "token" {
			token_key = strings.TrimSpace(t[1])
		} else if t[0] == "user" {
			user_key = strings.TrimSpace(t[1])
		}
	}

	if token_key == "" || user_key == "" {
		fmt.Println("Error processing 'token'/'user' from .pushover file.")
		os.Exit(1)
	}
}

// extractJSON extracts and returns a JSON structure.
func extractJSON(jsonBlob []byte) (map[string]interface{}, error) {
	var v interface{}

	err := json.Unmarshal(jsonBlob, &v)
	if err != nil {
		return nil, err
	}

	j := v.(map[string]interface{})

	return j, nil
}

func main() {
	// request payload.
	v := url.Values{}
	v.Set("token", token_key)
	v.Set("user", user_key)
	v.Set("message", message)

	if device != "" {
		v.Set("device", device)
	}

	if priority != "" {
		v.Set("priority", priority)
		if priority == "2" {
			// emergency priority notification.
			v.Set("retry", "60")
			v.Set("expire", "3600")
		}
	}

	if sound != "" {
		v.Set("sound", sound)
	}

	if timestamp != "" {
		v.Set("timestamp", timestamp)
	}

	if title != "" {
		v.Set("title", title)
	}

	if url_path != "" {
		v.Set("url", url_path)
	}

	if url_title != "" {
		v.Set("url_title", url_title)
	}

	p := v.Encode()

	req, err := http.NewRequest("POST",
		"https://api.pushover.net/1/messages.json",
		bytes.NewBufferString(p))

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	req.Header.Set("Host", "api.pushover.net")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Content-Length", strconv.Itoa(len(p)))

	c := &http.Client{}
	rsp, err := c.Do(req)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer rsp.Body.Close()
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if rsp.StatusCode == http.StatusOK {
		j, err := extractJSON(body)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		fmt.Println("Success")
		if verbose {
			fmt.Println("Request id:", j["request"])
			fmt.Println("Message limit:", rsp.Header["X-Limit-App-Limit"][0])
			fmt.Println("Remaining Messages:", rsp.Header["X-Limit-App-Remaining"][0])

			n, err := strconv.ParseInt(rsp.Header["X-Limit-App-Reset"][0], 10, 64)
			if err != nil {
				fmt.Println("Message limit resets on: ?")
			} else {
				t := time.Unix(n, 0)
				fmt.Println("Message limit resets on:", t.Format("Jan 2, 2006 at 3:04pm (UTC)"))
			}
		}
	} else if rsp.StatusCode >= 400 && rsp.StatusCode <= 499 {
		j, err := extractJSON(body)
		if err != nil {
			fmt.Println(err)
		} else {
			m := j["errors"].(interface{})
			fmt.Println("Fail", m)
		}
		os.Exit(1)
	} else {
		fmt.Println(http.StatusText(rsp.StatusCode))
		os.Exit(1)
	}
}
<commit_msg>Remove underscores from variable names<commit_after>package main

import (
	"bufio"
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"os"
	"os/user"
	"strconv"
	"strings"
	"time"
)

var (
	device    string
	message   string
	priority  string
	sound     string
	timestamp string
	title     string
	tokenKey  string
	urlPath   string
	urlTitle  string
	userKey   string
)

var verbose bool
var version bool

// Semantic versioning - http://semver.org/
const (
	Major = 1
	Minor = 0
	Patch = 0
)

// init process command line flags and read in user created api keys file.
func init() {
	flag.BoolVar(&version, "version", false, "Display program version.")

	flag.BoolVar(&verbose, "v", false, "Display message response details.")

	flag.StringVar(&device, "device", "", "Send message directly to this "+
		"device, rather than all devices.")

	flag.StringVar(&message, "msg", "", "(Required) - Your message.")

	flag.StringVar(&priority, "priority", "", "Message priority. -2 = lowest "+
		", -1 = low, 0 = normal, 1 = high, 2 = emergency.")

	flag.StringVar(&sound, "sound", "", "Sound to play when user receives "+
		"notification, overrides the user's default sound choice.")

	flag.StringVar(&timestamp, "timestamp", "", "A Unix timestamp of your "+
		"message's date and time to display to the user.")

	flag.StringVar(&title, "title", "", "Your message's title, otherwise "+
		"your app's name is used.")

	flag.StringVar(&urlPath, "url", "", "A supplementary URL to show with "+
		"your message.")

	flag.StringVar(&urlTitle, "url-title", "", "A title for your "+
		"supplementary URL, otherwise just the URL is shown.")

	flag.Parse()

	if version {
		fmt.Println("Pushover", fmt.Sprintf("%d.%d.%d", Major, Minor, Patch))
		os.Exit(0)
	}

	if message == "" {
		fmt.Println("-msg is a required flag. You must specify a message, " +
			"use -h for help.")
		os.Exit(1)
	}

	u, err := user.Current()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	file, err := os.Open(u.HomeDir + "/.pushover")
	if err != nil {
		fmt.Println(err)
		fmt.Println("You need to create a dot file '.pushover' in your home " +
			"directory, containing your token and user api keys, in the " +
			"format:\ntoken=XXXXXXXXXX\nuser=XXXXXXXXXX")
		os.Exit(1)
	}

	// read in user created api keys file.
	r := bufio.NewReaderSize(file, 50)
	for l, err := r.ReadString('\n'); err == nil; l, err = r.ReadString('\n') {
		t := strings.Split(l, "=")
		if t[0] == "token" {
			tokenKey = strings.TrimSpace(t[1])
		} else if t[0] == "user" {
			userKey = strings.TrimSpace(t[1])
		}
	}

	if tokenKey == "" || userKey == "" {
		fmt.Println("Error processing 'token'/'user' from .pushover file.")
		os.Exit(1)
	}
}

// extractJSON extracts and returns a JSON structure.
func extractJSON(jsonBlob []byte) (map[string]interface{}, error) {
	var v interface{}

	err := json.Unmarshal(jsonBlob, &v)
	if err != nil {
		return nil, err
	}

	j := v.(map[string]interface{})

	return j, nil
}

func main() {
	// request payload.
	v := url.Values{}
	v.Set("token", tokenKey)
	v.Set("user", userKey)
	v.Set("message", message)

	if device != "" {
		v.Set("device", device)
	}

	if priority != "" {
		v.Set("priority", priority)
		if priority == "2" {
			// emergency priority notification.
			v.Set("retry", "60")
			v.Set("expire", "3600")
		}
	}

	if sound != "" {
		v.Set("sound", sound)
	}

	if timestamp != "" {
		v.Set("timestamp", timestamp)
	}

	if title != "" {
		v.Set("title", title)
	}

	if urlPath != "" {
		v.Set("url", urlPath)
	}

	if urlTitle != "" {
		v.Set("urlTitle", urlTitle)
	}

	p := v.Encode()

	req, err := http.NewRequest("POST",
		"https://api.pushover.net/1/messages.json",
		bytes.NewBufferString(p))

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	req.Header.Set("Host", "api.pushover.net")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Content-Length", strconv.Itoa(len(p)))

	c := &http.Client{}
	rsp, err := c.Do(req)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer rsp.Body.Close()
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	if rsp.StatusCode == http.StatusOK {
		j, err := extractJSON(body)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		fmt.Println("Success")
		if verbose {
			fmt.Println("Request id:", j["request"])
			fmt.Println("Message limit:", rsp.Header["X-Limit-App-Limit"][0])
			fmt.Println("Remaining Messages:", rsp.Header["X-Limit-App-Remaining"][0])

			n, err := strconv.ParseInt(rsp.Header["X-Limit-App-Reset"][0], 10, 64)
			if err != nil {
				fmt.Println("Message limit resets on: ?")
			} else {
				t := time.Unix(n, 0)
				fmt.Println("Message limit resets on:", t.Format("Jan 2, 2006 at 3:04pm (UTC)"))
			}
		}
	} else if rsp.StatusCode >= 400 && rsp.StatusCode <= 499 {
		j, err := extractJSON(body)
		if err != nil {
			fmt.Println(err)
		} else {
			m := j["errors"].(interface{})
			fmt.Println("Fail", m)
		}
		os.Exit(1)
	} else {
		fmt.Println(http.StatusText(rsp.StatusCode))
		os.Exit(1)
	}
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"log"
	"net/http"
	"os"

	reaper "github.com/ramr/go-reaper"
)

// config stores the program configuration.
var config struct {
	Dsn           string
	ServerAddress string
}

// getEnv returns the value of the environment variable name or def if the
// variable is empty.
func getEnv(name, def string) (value string) {
	if value = os.Getenv(name); value == "" {
		value = def
	}
	return
}

func init() {
	// Initialise the flag options.
	flag.StringVar(&config.Dsn, "dsn", getEnv("OGO_ICAL_DSN", ""), "postgresql Data Source Name")
	flag.StringVar(&config.ServerAddress, "address", getEnv("OGO_ICAL_ADDRESS", ":8080"), "server address")
}

func main() {
	//  Start background reaping of orphaned child processes.
	go reaper.Reap()

	flag.Parse()

	// Connect to the data source.
	store := NewStore(config.Dsn)
	if err := store.Connect(); err != nil {
		log.Fatal(err)
	}

	// Start the HTTP server.
	handler := CalendarHandler(store)
	log.Printf("Starting server on %s", config.ServerAddress)
	log.Fatal(http.ListenAndServe(config.ServerAddress, handler))
}
<commit_msg>Add the -version command line flag.<commit_after>package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"os"

	reaper "github.com/ramr/go-reaper"
)

var (
	// config stores the program configuration.
	config struct {
		Dsn           string
		ServerAddress string
	}
	showVersion     bool
	version, commit string
)

// getEnv returns the value of the environment variable name or def if the
// variable is empty.
func getEnv(name, def string) (value string) {
	if value = os.Getenv(name); value == "" {
		value = def
	}
	return
}

func init() {
	// Initialise the flag options.
	flag.BoolVar(&showVersion, "version", false, "display version information")
	flag.StringVar(&config.Dsn, "dsn", getEnv("OGO_ICAL_DSN", ""), "postgresql Data Source Name")
	flag.StringVar(&config.ServerAddress, "address", getEnv("OGO_ICAL_ADDRESS", ":8080"), "server address")
}

func main() {
	//  Start background reaping of orphaned child processes.
	go reaper.Reap()

	flag.Parse()

	if showVersion {
		if version != "" && commit != "" {
			fmt.Printf("%s commit=%s\n", version, commit)
		} else {
			fmt.Println("No versioning information is available.")
		}
		os.Exit(0)
	}

	// Connect to the data source.
	store := NewStore(config.Dsn)
	if err := store.Connect(); err != nil {
		log.Fatal(err)
	}

	// Start the HTTP server.
	handler := CalendarHandler(store)
	log.Printf("Starting server on %s", config.ServerAddress)
	log.Fatal(http.ListenAndServe(config.ServerAddress, handler))
}
<|endoftext|>


<commit_before>package main

import (
	"flag"
	log "github.com/Sirupsen/logrus"
	config "github.com/pemcconnell/amald/config"
	"github.com/pemcconnell/amald/loaders"
	"github.com/pemcconnell/amald/notifiers"
	"github.com/pemcconnell/amald/storage"
)

const (
	VERSION string = "0.0.1"
)

var (
	configpath = flag.String("c", "./config.yaml",
		"[config] set the path for the yaml config file. This defautls to "+
			"./config.yaml")
	loglevel = flag.String("v", "info",
		"[loglevel] set the verbosity of the log levels. Can be: debug, "+
			"info, warn, panic, fatal")
)

func init() {
	// parse flags
	flag.Parse()

	// Set logrus level
	switch *loglevel {
	case "debug":
		log.SetLevel(log.DebugLevel)
	case "info":
		log.SetLevel(log.InfoLevel)
	case "warn":
		log.SetLevel(log.WarnLevel)
	case "panic":
		log.SetLevel(log.PanicLevel)
	case "fatal":
		log.SetLevel(log.FatalLevel)
	}
}

func main() {

	welcome()

	// load the config
	config, err := config.LoadConfig(*configpath)
	exitOnError(err, "There was a problem setting the config")

	// grab the loaders
	err = loaders.GetLoaders(config.ActiveLoaders)
	exitOnError(err, "Failed to grab loaders")

	// collect up some urls from the loaders
	urls, err := loaders.CollectUrls()
	exitOnError(err, "Failed to exec loaders")

	// storage
	jsonbytes, err := storage.StoreData(urls, config.Storage)
	exitOnError(err, "Failed to StoreData")

	// call the notifiers to display/send the messages
	err = notifiers.FireNotifiers(jsonbytes, config.Reports)
	exitOnError(err, "Failed to FireNotifiers()")
}

// exitOnError checks that and error is not nil. If the passed value is an
// error, it is logged and the program exits with an error code of 1
func exitOnError(err error, prefix string) {
	if err != nil {
		log.WithFields(log.Fields{"err": err}).Fatal(prefix)
	}
}

// welcome displays the version number to the user
func welcome() {
	log.Info("\n---------------------------------\n"+
		" ~ amald v"+VERSION, " ~\n"+
		"---------------------------------")
}
<commit_msg>Updated version<commit_after>package main

import (
	"flag"
	log "github.com/Sirupsen/logrus"
	config "github.com/pemcconnell/amald/config"
	"github.com/pemcconnell/amald/loaders"
	"github.com/pemcconnell/amald/notifiers"
	"github.com/pemcconnell/amald/storage"
)

const (
	VERSION string = "0.0.2"
)

var (
	configpath = flag.String("c", "./config.yaml",
		"[config] set the path for the yaml config file. This defautls to "+
			"./config.yaml")
	loglevel = flag.String("v", "info",
		"[loglevel] set the verbosity of the log levels. Can be: debug, "+
			"info, warn, panic, fatal")
)

func init() {
	// parse flags
	flag.Parse()

	// Set logrus level
	switch *loglevel {
	case "debug":
		log.SetLevel(log.DebugLevel)
	case "info":
		log.SetLevel(log.InfoLevel)
	case "warn":
		log.SetLevel(log.WarnLevel)
	case "panic":
		log.SetLevel(log.PanicLevel)
	case "fatal":
		log.SetLevel(log.FatalLevel)
	}
}

func main() {

	welcome()

	// load the config
	config, err := config.LoadConfig(*configpath)
	exitOnError(err, "There was a problem setting the config")

	// grab the loaders
	err = loaders.GetLoaders(config.ActiveLoaders)
	exitOnError(err, "Failed to grab loaders")

	// collect up some urls from the loaders
	urls, err := loaders.CollectUrls()
	exitOnError(err, "Failed to exec loaders")

	// storage
	jsonbytes, err := storage.StoreData(urls, config.Storage)
	exitOnError(err, "Failed to StoreData")

	// call the notifiers to display/send the messages
	err = notifiers.FireNotifiers(jsonbytes, config.Reports)
	exitOnError(err, "Failed to FireNotifiers()")
}

// exitOnError checks that and error is not nil. If the passed value is an
// error, it is logged and the program exits with an error code of 1
func exitOnError(err error, prefix string) {
	if err != nil {
		log.WithFields(log.Fields{"err": err}).Fatal(prefix)
	}
}

// welcome displays the version number to the user
func welcome() {
	log.Info("\n---------------------------------\n"+
		" ~ amald v"+VERSION, " ~\n"+
		"---------------------------------")
}
<|endoftext|>


<commit_before>package main

import (
	"os/exec"
	"strings"
	"time"

	"github.com/zenhack/my-status-monitor/battery"
	"github.com/zenhack/my-status-monitor/weather"
)

func setStatus(str string) {
	cmd := exec.Command("xsetroot", "-name", str)
	cmd.Run()
}

func main() {
	weatherChannel := weather.Monitor() // har har.
	batteryChannel := battery.Monitor("/sys/class/power_supply/BAT0/")
	ticker := time.NewTicker(time.Minute)
	timeFormat := "Mon Jan 2 3:04 PM"

	w, b := "", ""
	t := time.Now()
	tstring := t.Format(timeFormat)
	for {
		setStatus(strings.Join([]string{w, b, tstring}, " | "))

		select {
		case w = <-weatherChannel:
		case b = <-batteryChannel:
		case t = <-ticker.C:
			tstring = t.Format(timeFormat)
		}
	}
}
<commit_msg>Don't display empty strings<commit_after>package main

import (
	"os/exec"
	"strings"
	"time"

	"github.com/zenhack/my-status-monitor/battery"
	"github.com/zenhack/my-status-monitor/weather"
)

func setStatus(str string) {
	cmd := exec.Command("xsetroot", "-name", str)
	cmd.Run()
}

// Like strings.Join(strs, " | "), but filters out empty strings in strs first.
func join(strs ...string) string {
	resultSlice := make([]string, 0, len(strs))
	for i := range(strs) {
		if strs[i] != "" {
			resultSlice = append(resultSlice, strs[i])
		}
	}
	return strings.Join(resultSlice, " | ")
}

func main() {
	weatherChannel := weather.Monitor() // har har.
	batteryChannel := battery.Monitor("/sys/class/power_supply/BAT0/")
	ticker := time.NewTicker(time.Minute)
	timeFormat := "Mon Jan 2 3:04 PM"

	w, b := "", ""
	t := time.Now()
	tstring := t.Format(timeFormat)
	for {
		setStatus(join(w, b, tstring))

		select {
		case w = <-weatherChannel:
		case b = <-batteryChannel:
		case t = <-ticker.C:
			tstring = t.Format(timeFormat)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/facebookgo/grace/gracehttp"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"

	"github.com/eirka/eirka-libs/config"
	"github.com/eirka/eirka-libs/cors"
	"github.com/eirka/eirka-libs/db"
	"github.com/eirka/eirka-libs/redis"
	"github.com/eirka/eirka-libs/user"

	local "github.com/eirka/eirka-post/config"
	c "github.com/eirka/eirka-post/controllers"
	m "github.com/eirka/eirka-post/middleware"
	u "github.com/eirka/eirka-post/utils"
)

var (
	version = "0.9.1"
)

func init() {

	// Database connection settings
	dbase := db.Database{

		User:           local.Settings.Database.User,
		Password:       local.Settings.Database.Password,
		Proto:          local.Settings.Database.Proto,
		Host:           local.Settings.Database.Host,
		Database:       local.Settings.Database.Database,
		MaxIdle:        local.Settings.Database.MaxIdle,
		MaxConnections: local.Settings.Database.MaxConnections,
	}

	// Set up DB connection
	dbase.NewDb()

	// Get limits and stuff from database
	config.GetDatabaseSettings()

	// redis settings
	r := redis.Redis{
		// Redis address and max pool connections
		Protocol:       local.Settings.Redis.Protocol,
		Address:        local.Settings.Redis.Address,
		MaxIdle:        local.Settings.Redis.MaxIdle,
		MaxConnections: local.Settings.Redis.MaxConnections,
	}

	// Set up Redis connection
	r.NewRedisCache()

	// set auth middleware secret
	user.Secret = local.Settings.Session.Secret

	// print the starting info
	StartInfo()

	// Print out config
	config.Print()

	// Print out config
	local.Print()

	// check what services are available
	u.CheckServices()

	// Print capabilities
	u.Services.Print()

	// set cors domains
	cors.SetDomains(local.Settings.CORS.Sites, strings.Split("POST", ","))

}

func main() {
	r := gin.Default()

	r.Use(cors.CORS())

	r.GET("/uptime", c.UptimeController)
	r.NoRoute(c.ErrorController)

	// all users
	public := r.Group("/")
	public.Use(user.Auth(false))

	public.POST("/thread/new", m.GetAntiSpamCookie(), c.ThreadController)
	public.POST("/thread/reply", m.GetAntiSpamCookie(), c.ReplyController)
	public.POST("/tag/new", m.GetAntiSpamCookie(), c.NewTagController)
	public.POST("/tag/add", m.GetAntiSpamCookie(), c.AddTagController)
	public.POST("/register", c.RegisterController)
	public.POST("/login", c.LoginController)

	// requires user perms
	users := r.Group("/user")
	users.Use(user.Auth(true))

	users.POST("/favorite", c.FavoritesController)
	users.POST("/password", c.PasswordController)
	users.POST("/email", c.EmailController)

	s := &http.Server{
		Addr:    fmt.Sprintf("%s:%d", local.Settings.Post.Address, local.Settings.Post.Port),
		Handler: r,
	}

	gracehttp.Serve(s)

}

func StartInfo() {

	fmt.Println(strings.Repeat("*", 60))
	fmt.Printf("%-20v\n\n", "PRAM-POST")
	fmt.Printf("%-20v%40v\n", "Version", version)
	fmt.Println(strings.Repeat("*", 60))

}
<commit_msg>add csrf<commit_after>package main

import (
	"fmt"
	"github.com/facebookgo/grace/gracehttp"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"

	"github.com/eirka/eirka-libs/config"
	"github.com/eirka/eirka-libs/cors"
	"github.com/eirka/eirka-libs/csrf"
	"github.com/eirka/eirka-libs/db"
	"github.com/eirka/eirka-libs/redis"
	"github.com/eirka/eirka-libs/user"

	local "github.com/eirka/eirka-post/config"
	c "github.com/eirka/eirka-post/controllers"
	m "github.com/eirka/eirka-post/middleware"
	u "github.com/eirka/eirka-post/utils"
)

var (
	version = "0.9.1"
)

func init() {

	// Database connection settings
	dbase := db.Database{

		User:           local.Settings.Database.User,
		Password:       local.Settings.Database.Password,
		Proto:          local.Settings.Database.Proto,
		Host:           local.Settings.Database.Host,
		Database:       local.Settings.Database.Database,
		MaxIdle:        local.Settings.Database.MaxIdle,
		MaxConnections: local.Settings.Database.MaxConnections,
	}

	// Set up DB connection
	dbase.NewDb()

	// Get limits and stuff from database
	config.GetDatabaseSettings()

	// redis settings
	r := redis.Redis{
		// Redis address and max pool connections
		Protocol:       local.Settings.Redis.Protocol,
		Address:        local.Settings.Redis.Address,
		MaxIdle:        local.Settings.Redis.MaxIdle,
		MaxConnections: local.Settings.Redis.MaxConnections,
	}

	// Set up Redis connection
	r.NewRedisCache()

	// set auth middleware secret
	user.Secret = local.Settings.Session.Secret

	// print the starting info
	StartInfo()

	// Print out config
	config.Print()

	// Print out config
	local.Print()

	// check what services are available
	u.CheckServices()

	// Print capabilities
	u.Services.Print()

	// set cors domains
	cors.SetDomains(local.Settings.CORS.Sites, strings.Split("POST", ","))

}

func main() {
	r := gin.Default()

	r.Use(cors.CORS())
	// verified the csrf token from the request
	r.Use(csrf.Verify())

	r.GET("/uptime", c.UptimeController)
	r.NoRoute(c.ErrorController)

	// all users
	public := r.Group("/")
	public.Use(user.Auth(false))

	public.POST("/thread/new", m.GetAntiSpamCookie(), c.ThreadController)
	public.POST("/thread/reply", m.GetAntiSpamCookie(), c.ReplyController)
	public.POST("/tag/new", m.GetAntiSpamCookie(), c.NewTagController)
	public.POST("/tag/add", m.GetAntiSpamCookie(), c.AddTagController)
	public.POST("/register", c.RegisterController)
	public.POST("/login", c.LoginController)

	// requires user perms
	users := r.Group("/user")
	users.Use(user.Auth(true))

	users.POST("/favorite", c.FavoritesController)
	users.POST("/password", c.PasswordController)
	users.POST("/email", c.EmailController)

	s := &http.Server{
		Addr:    fmt.Sprintf("%s:%d", local.Settings.Post.Address, local.Settings.Post.Port),
		Handler: r,
	}

	gracehttp.Serve(s)

}

func StartInfo() {

	fmt.Println(strings.Repeat("*", 60))
	fmt.Printf("%-20v\n\n", "PRAM-POST")
	fmt.Printf("%-20v%40v\n", "Version", version)
	fmt.Println(strings.Repeat("*", 60))

}
<|endoftext|>


<commit_before>/*Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/natefinch/pie"
	"github.com/op/go-logging"
	"github.com/skyrings/bigfin/backend/cephapi/client"
	"github.com/skyrings/bigfin/provider"
	"github.com/skyrings/bigfin/tools/task"
	"github.com/skyrings/skyring-common/conf"
	"github.com/skyrings/skyring-common/db"
	"github.com/skyrings/skyring-common/tools/logger"
	"github.com/skyrings/skyring-common/tools/schedule"
	"net/rpc/jsonrpc"
	"os"
	"path/filepath"

	bigfin_conf "github.com/skyrings/bigfin/conf"
)

func main() {
	confStr := make([]byte, base64.StdEncoding.DecodedLen(len(os.Args[1])))
	l, _ := base64.StdEncoding.Decode(confStr, []byte(os.Args[1]))
	var config conf.SkyringCollection
	if err := json.Unmarshal([]byte(confStr[:l]), &config); err != nil {
		panic(fmt.Sprintf("Reading configurations failed. error: %v", err))
	}
	conf.SystemConfig = config

	var eventTypes = make(map[string]string)
	if err := json.Unmarshal([]byte(os.Args[2]), &eventTypes); err != nil {
		panic(fmt.Sprintf("Reading event types failed. error: %v", err))
	}
	provider.EventTypes = eventTypes

	var providerConfig conf.ProviderInfo
	if err := json.Unmarshal([]byte(os.Args[3]), &providerConfig); err != nil {
		panic(fmt.Sprintf("Reading provider configurations failed. error: %v", err))
	}
	bigfin_conf.ProviderConfig = providerConfig

	// Initialize the logger
	level, err := logging.LogLevel(config.Logging.Level.String())
	if err != nil {
		level = logging.DEBUG
	}
	if err := logger.Init(
		"bigfin",
		fmt.Sprintf("%s/bigfin.log", filepath.Dir(config.Logging.Filename)),
		true,
		level); err != nil {
		panic(fmt.Sprintf("log init failed. %s", err))
	}

	if err := db.InitDBSession(config.DBConfig); err != nil {
		logger.Get().Fatalf("Unable to initialize DB. error: %v", err)
	}

	//Initialize the DB provider
	if err := provider.InitializeDb(); err != nil {
		logger.Get().Error("Unable to initialize the DB provider: %s", err)
	}

	// Initialize the task manager
	if err := task.InitializeTaskManager(); err != nil {
		logger.Get().Fatalf("Failed to initialize task manager. error: %v", err)
	}

	if err := provider.InitMonitoringManager(); err != nil {
		logger.Get().Fatalf("Error initializing the monitoring manager: %v", err)
	}

	if err := provider.InitInstaller(); err != nil {
		logger.Get().Fatalf("Error initializing the Installer: %v", err)
	}

	schedule.InitShechuleManager()
	if err := provider.ScheduleRbdEventEmitter(); err != nil {
		logger.Get().Error("Error while initializing RbdEventer scheduler: %v", err)
	}

	// Initialize ceph http client
	client.InitCephApiSession()

	provd := &provider.CephProvider{}
	p := pie.NewProvider()
	if err := p.RegisterName(bigfin_conf.ProviderName, provd); err != nil {
		logger.Get().Fatalf("Failed to register plugin. error: %v", err)
	}
	p.ServeCodec(jsonrpc.NewServerCodec)
}
<commit_msg>bigfin: Modified the decrypting logic for configurations<commit_after>/*Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/natefinch/pie"
	"github.com/op/go-logging"
	"github.com/skyrings/bigfin/backend/cephapi/client"
	"github.com/skyrings/bigfin/provider"
	"github.com/skyrings/bigfin/tools/task"
	"github.com/skyrings/skyring-common/conf"
	"github.com/skyrings/skyring-common/db"
	"github.com/skyrings/skyring-common/models"
	"github.com/skyrings/skyring-common/tools/crypto"
	"github.com/skyrings/skyring-common/tools/logger"
	"github.com/skyrings/skyring-common/tools/schedule"
	"io/ioutil"
	"net/rpc/jsonrpc"
	"os"
	"path/filepath"

	bigfin_conf "github.com/skyrings/bigfin/conf"
)

func main() {
	confStr := make([]byte, base64.StdEncoding.DecodedLen(len(os.Args[1])))
	l, _ := base64.StdEncoding.Decode(confStr, []byte(os.Args[1]))

	key, err := ioutil.ReadFile(models.SKYRING_ENC_KEY_FILE)
	if err != nil {
		panic(fmt.Sprintf("Reading enc key failed. error: %v", err))
	}
	cfgs, err := crypto.Decrypt(key, []byte(confStr[:l]))

	var config conf.SkyringCollection
	if err := json.Unmarshal(cfgs, &config); err != nil {
		panic(fmt.Sprintf("Reading configurations failed. error: %v", err))
	}
	conf.SystemConfig = config

	var eventTypes = make(map[string]string)
	if err := json.Unmarshal([]byte(os.Args[2]), &eventTypes); err != nil {
		panic(fmt.Sprintf("Reading event types failed. error: %v", err))
	}
	provider.EventTypes = eventTypes

	var providerConfig conf.ProviderInfo
	if err := json.Unmarshal([]byte(os.Args[3]), &providerConfig); err != nil {
		panic(fmt.Sprintf("Reading provider configurations failed. error: %v", err))
	}
	bigfin_conf.ProviderConfig = providerConfig

	// Initialize the logger
	level, err := logging.LogLevel(config.Logging.Level.String())
	if err != nil {
		level = logging.DEBUG
	}
	if err := logger.Init(
		"bigfin",
		fmt.Sprintf("%s/bigfin.log", filepath.Dir(config.Logging.Filename)),
		true,
		level); err != nil {
		panic(fmt.Sprintf("log init failed. %s", err))
	}

	if err := db.InitDBSession(config.DBConfig); err != nil {
		logger.Get().Fatalf("Unable to initialize DB. error: %v", err)
	}

	//Initialize the DB provider
	if err := provider.InitializeDb(); err != nil {
		logger.Get().Error("Unable to initialize the DB provider: %s", err)
	}

	// Initialize the task manager
	if err := task.InitializeTaskManager(); err != nil {
		logger.Get().Fatalf("Failed to initialize task manager. error: %v", err)
	}

	if err := provider.InitMonitoringManager(); err != nil {
		logger.Get().Fatalf("Error initializing the monitoring manager: %v", err)
	}

	if err := provider.InitInstaller(); err != nil {
		logger.Get().Fatalf("Error initializing the Installer: %v", err)
	}

	schedule.InitShechuleManager()
	if err := provider.ScheduleRbdEventEmitter(); err != nil {
		logger.Get().Error("Error while initializing RbdEventer scheduler: %v", err)
	}

	// Initialize ceph http client
	client.InitCephApiSession()

	provd := &provider.CephProvider{}
	p := pie.NewProvider()
	if err := p.RegisterName(bigfin_conf.ProviderName, provd); err != nil {
		logger.Get().Fatalf("Failed to register plugin. error: %v", err)
	}
	p.ServeCodec(jsonrpc.NewServerCodec)
}
<|endoftext|>


<commit_before>// elvish is an experimental Unix shell. It tries to incorporate a powerful
// programming language with an extensible, friendly user interface.
package main

import (
	"fmt"
	"os"
	"os/signal"
	"os/user"

	"github.com/elves/elvish/edit"
	"github.com/elves/elvish/errutil"
	"github.com/elves/elvish/eval"
	"github.com/elves/elvish/parse"
	"github.com/elves/elvish/store"
	"github.com/elves/elvish/util"
)

const (
	sigchSize     = 32
	outChanSize   = 32
	outChanLeader = "▶ "
)

func newEvaluator() *eval.Evaluator {
	ch := make(chan eval.Value, outChanSize)
	go func() {
		for v := range ch {
			fmt.Printf("%s%s\n", outChanLeader, v.Repr())
		}
	}()

	st, err := store.NewStore()
	if err != nil {
		fmt.Fprintln(os.Stderr, "Warning: cannot connect to store:", err)
	}

	ev := eval.NewEvaluator(st)
	ev.SetChanOut(ch)
	return ev
}

func printError(err error) {
	if err != nil {
		if ce, ok := err.(*errutil.ContextualError); ok {
			fmt.Fprint(os.Stderr, ce.Pprint())
		} else {
			fmt.Fprintln(os.Stderr, err.Error())
		}
	}
}

// TODO(xiaq): Currently only the editor deals with signals.
func interact() {
	ev := newEvaluator()
	datadir, err := store.EnsureDataDir()
	printError(err)
	if err == nil {
		err := ev.Source(datadir + "/rc.elv")
		if err != nil && !os.IsNotExist(err) {
			printError(err)
		}
	}

	cmdNum := 0

	username := "???"
	user, err := user.Current()
	if err == nil {
		username = user.Username
	}
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "???"
	}
	rpromptStr := username + "@" + hostname

	sigch := make(chan os.Signal, sigchSize)
	signal.Notify(sigch)

	ed := edit.NewEditor(os.Stdin, ev, sigch)

	for {
		cmdNum++
		name := fmt.Sprintf("<tty %d>", cmdNum)

		prompt := func() string {
			return util.Getwd() + "> "
		}
		rprompt := func() string {
			return rpromptStr
		}

		lr := ed.ReadLine(prompt, rprompt)

		if lr.EOF {
			break
		} else if lr.Err != nil {
			fmt.Println("Editor error:", lr.Err)
			fmt.Println("My pid is", os.Getpid())
		}

		n, pe := parse.Parse(name, lr.Line)
		printError(pe)

		ee := ev.Eval(name, lr.Line, n)
		printError(ee)
	}
}

func script(fname string) {
	ev := newEvaluator()
	err := ev.Source(fname)
	printError(err)
	if err != nil {
		os.Exit(1)
	}
}

var usage = `Usage:
    elvish
    elvish <script>
`

func main() {
	switch len(os.Args) {
	case 1:
		interact()
	case 2:
		script(os.Args[1])
	default:
		fmt.Fprintf(os.Stderr, usage)
		os.Exit(1)
	}
}
<commit_msg>Don't evaluate if parsing failed<commit_after>// elvish is an experimental Unix shell. It tries to incorporate a powerful
// programming language with an extensible, friendly user interface.
package main

import (
	"fmt"
	"os"
	"os/signal"
	"os/user"

	"github.com/elves/elvish/edit"
	"github.com/elves/elvish/errutil"
	"github.com/elves/elvish/eval"
	"github.com/elves/elvish/parse"
	"github.com/elves/elvish/store"
	"github.com/elves/elvish/util"
)

const (
	sigchSize     = 32
	outChanSize   = 32
	outChanLeader = "▶ "
)

func newEvaluator() *eval.Evaluator {
	ch := make(chan eval.Value, outChanSize)
	go func() {
		for v := range ch {
			fmt.Printf("%s%s\n", outChanLeader, v.Repr())
		}
	}()

	st, err := store.NewStore()
	if err != nil {
		fmt.Fprintln(os.Stderr, "Warning: cannot connect to store:", err)
	}

	ev := eval.NewEvaluator(st)
	ev.SetChanOut(ch)
	return ev
}

func printError(err error) {
	if err != nil {
		if ce, ok := err.(*errutil.ContextualError); ok {
			fmt.Fprint(os.Stderr, ce.Pprint())
		} else {
			fmt.Fprintln(os.Stderr, err.Error())
		}
	}
}

// TODO(xiaq): Currently only the editor deals with signals.
func interact() {
	ev := newEvaluator()
	datadir, err := store.EnsureDataDir()
	printError(err)
	if err == nil {
		err := ev.Source(datadir + "/rc.elv")
		if err != nil && !os.IsNotExist(err) {
			printError(err)
		}
	}

	cmdNum := 0

	username := "???"
	user, err := user.Current()
	if err == nil {
		username = user.Username
	}
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "???"
	}
	rpromptStr := username + "@" + hostname

	sigch := make(chan os.Signal, sigchSize)
	signal.Notify(sigch)

	ed := edit.NewEditor(os.Stdin, ev, sigch)

	for {
		cmdNum++
		name := fmt.Sprintf("<tty %d>", cmdNum)

		prompt := func() string {
			return util.Getwd() + "> "
		}
		rprompt := func() string {
			return rpromptStr
		}

		lr := ed.ReadLine(prompt, rprompt)

		if lr.EOF {
			break
		} else if lr.Err != nil {
			fmt.Println("Editor error:", lr.Err)
			fmt.Println("My pid is", os.Getpid())
		}

		n, pe := parse.Parse(name, lr.Line)
		printError(pe)

		if pe == nil {
			ee := ev.Eval(name, lr.Line, n)
			printError(ee)
		}
	}
}

func script(fname string) {
	ev := newEvaluator()
	err := ev.Source(fname)
	printError(err)
	if err != nil {
		os.Exit(1)
	}
}

var usage = `Usage:
    elvish
    elvish <script>
`

func main() {
	switch len(os.Args) {
	case 1:
		interact()
	case 2:
		script(os.Args[1])
	default:
		fmt.Fprintf(os.Stderr, usage)
		os.Exit(1)
	}
}
<|endoftext|>