Example #1
0
func WriteConfig(config *Config) {
	buf := &bytes.Buffer{}
	err := json.Marshal(buf, *config)
	if err != nil {
		log.Exitf("Failed to marshal config file: %s\n", err)
	}

	// TODO atomic update without truncating file
	err = io.WriteFile(configFilename, buf.Bytes(), configPerms)
	if err != nil {
		log.Exitf("Can't write config file: %s\n", err)
	}
}
Example #2
0
func savePaste(source string, private bool) string {
	var paste string

	if private {
		paste = newName("private:")
	} else {
		paste = newName("")
	}

	io.WriteFile(PATH+paste, strings.Bytes(source), 0644)

	return paste
}
Example #3
0
func processFile(filename string) os.Error {
	src, err := io.ReadFile(filename)
	if err != nil {
		return err
	}

	file, err := parser.ParseFile(filename, src, parserMode())
	if err != nil {
		return err
	}

	var res bytes.Buffer
	_, err = (&printer.Config{printerMode(), *tabwidth, nil}).Fprint(&res, file)
	if err != nil {
		return err
	}

	if bytes.Compare(src, res.Bytes()) != 0 {
		// formatting has changed
		if *list {
			fmt.Fprintln(os.Stdout, filename)
		}
		if *write {
			err = io.WriteFile(filename, res.Bytes(), 0)
			if err != nil {
				return err
			}
		}
	}

	if !*list && !*write {
		_, err = os.Stdout.Write(res.Bytes())
	}

	return err
}
Example #4
0
func (ps *PersistService) New(id string, data map[string]string) *Model {
	// TODO: malicious id path
	log.Stderrf(":NEW:id:%s", id);
	for k,v := range data {
		log.Stderrf("NEW:k:%s:v:%s", k, v);
	}

	m := new(Model);
	m.Id = id;
	m.Data = data;

	bs := unparse_model_data(m.Data);

	mpath := path.Join(ps.Path, id);
	e := io.WriteFile(mpath, bs, syscall.S_IWUSR|syscall.S_IRUSR); // 00200|00400); // os.O_WRONLY|os.O_CREATE|os.O_TRUNC);
	if e != nil {
		// TODO: better error handling
		return nil;
	}
	else {
		return m;
	}
	panic("unreachable");
}
Example #5
0
func check(t *testing.T, source, golden string, mode checkMode) {
	// parse source
	prog, err := parser.ParseFile(source, nil, parser.ParseComments)
	if err != nil {
		t.Error(err)
		return
	}

	// filter exports if necessary
	if mode&export != 0 {
		ast.FileExports(prog) // ignore result
		prog.Comments = nil   // don't print comments that are not in AST
	}

	// determine printer configuration
	cfg := Config{Tabwidth: tabwidth}
	if mode&rawFormat != 0 {
		cfg.Mode |= RawFormat
	}

	// format source
	var buf bytes.Buffer
	if _, err := cfg.Fprint(&buf, prog); err != nil {
		t.Error(err)
	}
	res := buf.Bytes()

	// update golden files if necessary
	if *update {
		if err := io.WriteFile(golden, res, 0644); err != nil {
			t.Error(err)
		}
		return
	}

	// get golden
	gld, err := io.ReadFile(golden)
	if err != nil {
		t.Error(err)
		return
	}

	// compare lengths
	if len(res) != len(gld) {
		t.Errorf("len = %d, expected %d (= len(%s))", len(res), len(gld), golden)
	}

	// compare contents
	for i, line, offs := 0, 1, 0; i < len(res) && i < len(gld); i++ {
		ch := res[i]
		if ch != gld[i] {
			t.Errorf("%s:%d:%d: %s", source, line, i-offs+1, lineString(res, offs))
			t.Errorf("%s:%d:%d: %s", golden, line, i-offs+1, lineString(gld, offs))
			t.Error()
			return
		}
		if ch == '\n' {
			line++
			offs = i + 1
		}
	}
}
Example #6
0
func main() {
	flag.Usage = usage
	flag.Parse()

	args := flag.Args()
	var data []byte
	var err os.Error
	switch len(args) {
	case 0:
		data, err = io.ReadAll(os.Stdin)
	case 1:
		data, err = io.ReadFile(args[0])
	default:
		usage()
	}
	chk(err)

	pset, err := patch.Parse(data)
	chk(err)

	// Change to hg root directory, because
	// patch paths are relative to root.
	root, err := hgRoot()
	chk(err)
	chk(os.Chdir(root))

	// Make sure there are no pending changes on the server.
	if *checkSync && hgIncoming() {
		fmt.Fprintf(os.Stderr, "incoming changes waiting; run hg sync first\n")
		os.Exit(2)
	}

	// Make sure we won't be editing files with local pending changes.
	dirtylist, err := hgModified()
	chk(err)
	dirty := make(map[string]int)
	for _, f := range dirtylist {
		dirty[f] = 1
	}
	conflict := make(map[string]int)
	for _, f := range pset.File {
		if f.Verb == patch.Delete || f.Verb == patch.Rename {
			if _, ok := dirty[f.Src]; ok {
				conflict[f.Src] = 1
			}
		}
		if f.Verb != patch.Delete {
			if _, ok := dirty[f.Dst]; ok {
				conflict[f.Dst] = 1
			}
		}
	}
	if len(conflict) > 0 {
		fmt.Fprintf(os.Stderr, "cannot apply patch to locally modified files:\n")
		for name := range conflict {
			fmt.Fprintf(os.Stderr, "\t%s\n", name)
		}
		os.Exit(2)
	}

	// Apply changes in memory.
	op, err := pset.Apply(io.ReadFile)
	chk(err)

	// Write changes to disk copy: order of commands matters.
	// Accumulate undo log as we go, in case there is an error.
	// Also accumulate list of modified files to print at end.
	changed := make(map[string]int)

	// Copy, Rename create the destination file, so they
	// must happen before we write the data out.
	// A single patch may have a Copy and a Rename
	// with the same source, so we have to run all the
	// Copy in one pass, then all the Rename.
	for i := range op {
		o := &op[i]
		if o.Verb == patch.Copy {
			makeParent(o.Dst)
			chk(hgCopy(o.Dst, o.Src))
			undoRevert(o.Dst)
			changed[o.Dst] = 1
		}
	}
	for i := range op {
		o := &op[i]
		if o.Verb == patch.Rename {
			makeParent(o.Dst)
			chk(hgRename(o.Dst, o.Src))
			undoRevert(o.Dst)
			undoRevert(o.Src)
			changed[o.Src] = 1
			changed[o.Dst] = 1
		}
	}

	// Run Delete before writing to files in case one of the
	// deleted paths is becoming a directory.
	for i := range op {
		o := &op[i]
		if o.Verb == patch.Delete {
			chk(hgRemove(o.Src))
			undoRevert(o.Src)
			changed[o.Src] = 1
		}
	}

	// Write files.
	for i := range op {
		o := &op[i]
		if o.Verb == patch.Delete {
			continue
		}
		if o.Verb == patch.Add {
			makeParent(o.Dst)
			changed[o.Dst] = 1
		}
		if o.Data != nil {
			chk(io.WriteFile(o.Dst, o.Data, 0644))
			if o.Verb == patch.Add {
				undoRm(o.Dst)
			} else {
				undoRevert(o.Dst)
			}
			changed[o.Dst] = 1
		}
		if o.Mode != 0 {
			chk(os.Chmod(o.Dst, o.Mode&0755))
			undoRevert(o.Dst)
			changed[o.Dst] = 1
		}
	}

	// hg add looks at the destination file, so it must happen
	// after we write the data out.
	for i := range op {
		o := &op[i]
		if o.Verb == patch.Add {
			chk(hgAdd(o.Dst))
			undoRevert(o.Dst)
			changed[o.Dst] = 1
		}
	}

	// Finished editing files.  Write the list of changed files to stdout.
	list := make([]string, len(changed))
	i := 0
	for f := range changed {
		list[i] = f
		i++
	}
	sort.SortStrings(list)
	for _, f := range list {
		fmt.Printf("%s\n", f)
	}
}
Example #7
0
File: main.go Project: ox/GoStones
func main() {
	flag.Parse()
	errors := vector.New(0)

	//fill repositories StringVector
	repositories := vector.New(0) //make a new vec
	file, err := os.Open("./list.txt", os.O_RDONLY, 0755)
	if file == nil {
		errors.Push(err)
	}

	in = bufio.NewReader(file)
	ik := 0

	for {
		dat2, err := in.ReadSlice('\n')
		if err == os.EOF || (string(dat2[0:len(dat2)-1]) == "" && err == os.EOF) {
			errors.Push(err)
			break
		}

		if string(dat2[0]) != "#" && string(dat2[0]) != "" && string(dat2[0]) != "\n" { //# is a comment
			str := strings.Split(string(dat2), " ", -1)
			gem := NewGemSource(str[1:len(str)], str[0])
			repositories.Push(gem)

			ik++
		}
	}

	//get the version number
	if *get_go_gems_version {
		go_gems_version, err := io.ReadFile("./go_stones_version")

		if go_gems_version == nil {
			errors.Push(err)
		}
		fmt.Println("GoStones version ", string(go_gems_version))

		br()
	}

	//list repositories
	if *list {
		fmt.Println("listing", ik, "repos:")
		for i := 0; i < ik; i++ {
			fmt.Printf("%v\n", repositories.At(i).(*GemSource).url)
		}
		br()
	}

	if *list_full {
		for i := 0; i < ik; i++ {
			fmt.Printf("%v\n----------------------\n    ", repositories.At(i).(*GemSource))
			for _, v := range repositories.At(i).(*GemSource).short_names {
				fmt.Printf("%v\n    ", v)
			}
			br()
		}
	}

	if *remove_repository != "" {

		fmt.Println("looking for", *remove_repository)

		l := 0

		for ; l < ik; l++ {
			gem := repositories.At(l).(*GemSource)

			if gem.url == *remove_repository {
				fmt.Print("found! removing ", gem.url, " at ", l, "\n")
				repositories.Delete(l)
				break
			}

			for _, v := range gem.short_names {
				if v == *remove_repository || v == *remove_repository+"\n" {
					fmt.Print("found! removing ", v, " at ", l, "\n")
					repositories.Delete(l)
					l = ik
					break
				}
			}
		}

		if repositories.Len() == ik { //nothing was removed
			//our fall through
			fmt.Println("No such alias or url found in the list, check spelling or url,", repositories.Len())
			os.Exit(1)
		}

		file, err := os.Open("./list.txt", os.O_RDWR, 0755)
		if file == nil {
			errors.Push(err)
		}

		out = bufio.NewWriter(file)

		errlol := io.WriteFile(file.Name(), []byte{}, 0755)
		if errlol != nil {
			fmt.Print(errlol)
			errors.Push(errlol)
			os.Exit(1)
		}

		for i := 0; i < repositories.Len(); i++ {
			gem := repositories.At(i).(*GemSource)
			io.WriteString(out, gem.url)
			fmt.Print(gem)

			for k := 0; k < len(gem.short_names); k++ {
				io.WriteString(out, " "+gem.short_names[k])
				fmt.Print(" " + gem.short_names[k])
			}
			//io.WriteString(out, "\n" );
		}

		out.Flush()
		file.Close()
	}

	if *add_repo != "" {
		str := strings.Split(*add_repo, " ", -1)

		var gem *GemSource

		if len(str) == 1 {
			_, short_name := path.Split(str[0])
			short_name = strings.Split(short_name, ".", -1)[0]
			gem = NewGemSource([]string{short_name + "\n"}, str[0])
			fmt.Println("no alias provided, making it", short_name)
		} else {
			gem = NewGemSource(str[1:len(str)], str[0])
		}

		fmt.Println("adding:", gem)

		repositories.Push(gem)

		file, err := os.Open("./list.txt", os.O_RDWR, 0755)
		if file == nil {
			errors.Push(err)
		}

		out = bufio.NewWriter(file)

		for i := 0; i < repositories.Len(); i++ {
			gem := repositories.At(i).(*GemSource)
			io.WriteString(out, gem.url)
			for k := 0; k < len(gem.short_names); k++ {
				io.WriteString(out, " "+gem.short_names[k])
			}
		}
		io.WriteString(out, "\n")

		out.Flush()
		file.Close()
	}

	if *install != "" {
		file_name := *install

		//search the repos for the right file
		for i := 0; i < ik; i++ {
			gem := repositories.At(i).(*GemSource)
			_, pre_tested_name := path.Split(gem.short_names[0])
			tested_name := strings.Split(pre_tested_name, ".", -1)[0]

			for _, val := range gem.short_names {

				if tested_name == file_name || val == file_name || val == file_name+"\n" {
					str := strings.Split(gem.url, ":", -1)

					fmt.Println(str[0])

					switch str[0] {
					case "http":
						fmt.Println("Pulling from the net...")

						response, _, err := http.Get(gem.url)
						if err != nil {
							fmt.Println(err)
							os.Exit(1)
						}

						var nr int
						const buf_size = 0x10
						buf := make([]byte, buf_size)
						nr, _ = response.Body.Read(buf)

						if nr >= buf_size {
							panic("Buffer overrun")
						}

						errorr := io.WriteFile("./"+pre_tested_name, buf, 0755)
						if errorr != nil {
							fmt.Println(errorr)
							os.Exit(1)
						}

						buf = nil
						response.Body.Close()
						break

					case "git":
						fmt.Println("git'n it from the net...")

						dir := git_from_net(string(gem.url))
						log.Stdout(dir)
						build_pkg(string(dir))

						break
					}

					fmt.Println("passed retrieving file...")

					break
				}
			}
		}
	}

	if *show_errors {
		fmt.Println(errors)
	}

	file.Close()

}