Skip to content

Commit

Permalink
Added file suffix for tmp file, to support syntax highlighting. Updat…
Browse files Browse the repository at this point in the history
…ed vendoring
  • Loading branch information
Michael Persson committed Feb 16, 2016
1 parent 6d43f8f commit 69065e3
Show file tree
Hide file tree
Showing 40 changed files with 1,219 additions and 156 deletions.
17 changes: 10 additions & 7 deletions src/github.com/mickep76/etcdtool/command/edit_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewEditCommand() cli.Command {
cli.BoolFlag{Name: "validate, v", EnvVar: "ETCDTOOL_VALIDATE", Usage: "Validate data before import"},
cli.StringFlag{Name: "format, f", Value: "JSON", EnvVar: "ETCDTOOL_FORMAT", Usage: "Data serialization format YAML, TOML or JSON"},
cli.StringFlag{Name: "editor, e", Value: "vim", Usage: "Editor", EnvVar: "EDITOR"},
cli.StringFlag{Name: "tmp-file, t", Value: ".etcdtool.swp", Usage: "Temporary file"},
cli.StringFlag{Name: "tmp-file, t", Value: ".etcdtool", Usage: "Temporary file"},
},
Action: func(c *cli.Context) {
editCommandFunc(c)
Expand Down Expand Up @@ -72,33 +72,36 @@ func editCommandFunc(c *cli.Context) {
fatal(err.Error())
}

// Temporary file append file type to support syntax highlighting
tmpfile := c.String("tmp-file") + "." + strings.ToLower(c.String("format"))

// Export to file.
exportFunc(dir, sort, c.String("tmp-file"), f, c, ki)
exportFunc(dir, sort, tmpfile, f, c, ki)

// Get modified time stamp.
before, err := os.Stat(c.String("tmp-file"))
before, err := os.Stat(tmpfile)
if err != nil {
fatal(err.Error())
}

// Edit file.
editFile(c.String("editor"), c.String("tmp-file"))
editFile(c.String("editor"), tmpfile)

// Check modified time stamp.
after, err := os.Stat(c.String("tmp-file"))
after, err := os.Stat(tmpfile)
if err != nil {
fatal(err.Error())
}

// Import from file if it has changed.
if before.ModTime() != after.ModTime() {
importFunc(dir, c.String("tmp-file"), f, c.Bool("replace"), c.Bool("yes"), e, c, ki)
importFunc(dir, tmpfile, f, c.Bool("replace"), c.Bool("yes"), e, c, ki)
} else {
fmt.Printf("File wasn't modified, skipping import\n")
}

// Unlink file.
if err := os.Remove(c.String("tmp-file")); err != nil {
if err := os.Remove(tmpfile); err != nil {
fatal(err.Error())
}
}
2 changes: 1 addition & 1 deletion src/github.com/mickep76/etcdtool/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main

// Version for app.
const Version = "2.9"
const Version = "3.0"
22 changes: 11 additions & 11 deletions vendor/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"importpath": "github.com/BurntSushi/toml",
"repository": "https://github.com/BurntSushi/toml",
"revision": "056c9bc7be7190eaa7715723883caffa5f8fa3e4",
"revision": "5c4df71dfe9ac89ef6287afc05e4c1b16ae65a1e",
"branch": "master"
},
{
Expand All @@ -16,61 +16,61 @@
{
"importpath": "github.com/codegangsta/cli",
"repository": "https://github.com/codegangsta/cli",
"revision": "b5232bb2934f606f9f27a1305f1eea224e8e8b88",
"revision": "0ab42fd482c27cf2c95e7794ad3bb2082c2ab2d7",
"branch": "master"
},
{
"importpath": "github.com/coreos/etcd/Godeps/_workspace/src/github.com/ugorji/go/codec",
"repository": "https://github.com/coreos/etcd",
"revision": "9b0b15c9be660695082d683bee6d0b5ea59904c4",
"revision": "30e4d7d6aa97040c5040ffd2344ac0d6effe3d23",
"branch": "master",
"path": "/Godeps/_workspace/src/github.com/ugorji/go/codec"
},
{
"importpath": "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context",
"repository": "https://github.com/coreos/etcd",
"revision": "9b0b15c9be660695082d683bee6d0b5ea59904c4",
"revision": "30e4d7d6aa97040c5040ffd2344ac0d6effe3d23",
"branch": "master",
"path": "/Godeps/_workspace/src/golang.org/x/net/context"
},
{
"importpath": "github.com/coreos/etcd/client",
"repository": "https://github.com/coreos/etcd",
"revision": "9b0b15c9be660695082d683bee6d0b5ea59904c4",
"revision": "30e4d7d6aa97040c5040ffd2344ac0d6effe3d23",
"branch": "master",
"path": "/client"
},
{
"importpath": "github.com/coreos/etcd/pkg/pathutil",
"repository": "https://github.com/coreos/etcd",
"revision": "9b0b15c9be660695082d683bee6d0b5ea59904c4",
"revision": "30e4d7d6aa97040c5040ffd2344ac0d6effe3d23",
"branch": "master",
"path": "/pkg/pathutil"
},
{
"importpath": "github.com/coreos/etcd/pkg/transport",
"repository": "https://github.com/coreos/etcd",
"revision": "9b0b15c9be660695082d683bee6d0b5ea59904c4",
"revision": "30e4d7d6aa97040c5040ffd2344ac0d6effe3d23",
"branch": "master",
"path": "/pkg/transport"
},
{
"importpath": "github.com/coreos/etcd/pkg/types",
"repository": "https://github.com/coreos/etcd",
"revision": "9b0b15c9be660695082d683bee6d0b5ea59904c4",
"revision": "30e4d7d6aa97040c5040ffd2344ac0d6effe3d23",
"branch": "master",
"path": "/pkg/types"
},
{
"importpath": "github.com/mickep76/etcdmap",
"repository": "https://github.com/mickep76/etcdmap",
"revision": "7128b59ea24bc4ad44b1eb0e1c9998459c01fc80",
"revision": "7251bb03b7f4fe0199e096014bb5b61db0348e91",
"branch": "master"
},
{
"importpath": "github.com/mickep76/iodatafmt",
"repository": "https://github.com/mickep76/iodatafmt",
"revision": "b597e16ba86d5b61a9fc26e0d7cfa92682009e1d",
"revision": "b8a6f28eabf7e2ece95de10eadbbe88c2e065081",
"branch": "master"
},
{
Expand All @@ -94,7 +94,7 @@
{
"importpath": "golang.org/x/net/context",
"repository": "https://go.googlesource.com/net",
"revision": "d28a91ad269180318493156412990f060d721258",
"revision": "cbbbe2bc0f2efdd2afb318d93f1eadb19350e4a3",
"branch": "master",
"path": "/context"
},
Expand Down
2 changes: 0 additions & 2 deletions vendor/src/github.com/BurntSushi/toml/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@ func lexTableNameStart(lx *lexer) stateFn {
lx.ignore()
lx.push(lexTableNameEnd)
return lexValue // reuse string lexing
case isWhitespace(r):
return lexTableNameStart
default:
return lexBareTableName
}
Expand Down
17 changes: 14 additions & 3 deletions vendor/src/github.com/codegangsta/cli/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
[![Coverage](http://gocover.io/_badge/github.com/codegangsta/cli?0)](http://gocover.io/github.com/codegangsta/cli)
[![Build Status](https://travis-ci.org/codegangsta/cli.png?branch=master)](https://travis-ci.org/codegangsta/cli)
[![Build Status](https://travis-ci.org/codegangsta/cli.svg?branch=master)](https://travis-ci.org/codegangsta/cli)
[![GoDoc](https://godoc.org/github.com/codegangsta/cli?status.svg)](https://godoc.org/github.com/codegangsta/cli)

# cli.go

`cli.go` is simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable command line applications in an expressive way.

## Overview

Command line apps are usually so tiny that there is absolutely no reason why your code should *not* be self-documenting. Things like generating help text and parsing command flags/options should not hinder productivity when writing a command line app.

**This is where `cli.go` comes into play.** `cli.go` makes command line programming fun, organized, and expressive!

## Installation

Make sure you have a working Go environment (go 1.1+ is *required*). [See the install instructions](http://golang.org/doc/install.html).

To install `cli.go`, simply run:
Expand All @@ -24,6 +27,7 @@ export PATH=$PATH:$GOPATH/bin
```

## Getting Started

One of the philosophies behind `cli.go` is that an API should be playful and full of discovery. So a `cli.go` app can be as little as one line of code in `main()`.

``` go
Expand Down Expand Up @@ -123,6 +127,7 @@ GLOBAL OPTIONS
```

### Arguments

You can lookup arguments by calling the `Args` function on `cli.Context`.

``` go
Expand All @@ -134,7 +139,9 @@ app.Action = func(c *cli.Context) {
```

### Flags

Setting and querying flags is simple.

``` go
...
app.Flags = []cli.Flag {
Expand All @@ -159,6 +166,7 @@ app.Action = func(c *cli.Context) {
```

You can also set a destination variable for a flag, to which the content will be scanned.

``` go
...
var language string
Expand Down Expand Up @@ -233,6 +241,7 @@ app.Flags = []cli.Flag {
### Subcommands

Subcommands can be defined for a more git-like command line app.

```go
...
app.Commands = []cli.Command{
Expand Down Expand Up @@ -283,6 +292,7 @@ You can enable completion commands by setting the `EnableBashCompletion`
flag on the `App` object. By default, this setting will only auto-complete to
show an app's subcommands, but you can write your own completion methods for
the App or its subcommands.

```go
...
var tasks = []string{"cook", "clean", "laundry", "eat", "sleep", "code"}
Expand Down Expand Up @@ -325,15 +335,16 @@ automatically install it there if you are distributing a package). Don't forget
to source the file to make it active in the current shell.

```
sudo cp src/bash_autocomplete /etc/bash_completion.d/<myprogram>
source /etc/bash_completion.d/<myprogram>
sudo cp src/bash_autocomplete /etc/bash_completion.d/<myprogram>
source /etc/bash_completion.d/<myprogram>
```

Alternatively, you can just document that users should source the generic
`autocomplete/bash_autocomplete` in their bash configuration with `$PROG` set
to the name of their program (as above).

## Contribution Guidelines

Feel free to put up a pull request to fix a bug or maybe add a feature. I will give it a code review and make sure that it does not break backwards compatibility. If I or any other collaborators agree that it is in line with the vision of the project, we will work with you to get the code into a mergeable state and merge it into the master branch.

If you have contributed something significant to the project, I will most likely add you as a collaborator. As a collaborator you are given the ability to merge others pull requests. It is very important that new code does not break existing code, so be careful about what code you do choose to merge. If you have any questions feel free to link @codegangsta to the issue in question and we can review it together.
Expand Down
50 changes: 33 additions & 17 deletions vendor/src/github.com/codegangsta/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
"io"
"io/ioutil"
"os"
"path"
"time"
)

// App is the main structure of a cli application. It is recomended that
// App is the main structure of a cli application. It is recommended that
// an app be created with the cli.NewApp() function
type App struct {
// The name of the program. Defaults to os.Args[0]
// The name of the program. Defaults to path.Base(os.Args[0])
Name string
// Full name of command for help, defaults to Name
HelpName string
// Description of the program.
Usage string
// Text to override the USAGE section of help
UsageText string
// Description of the program argument format.
ArgsUsage string
// Version of the program
Expand All @@ -43,6 +46,10 @@ type App struct {
Action func(context *Context)
// Execute this function if the proper command cannot be found
CommandNotFound func(context *Context, command string)
// Execute this function, if an usage error occurs. This is useful for displaying customized usage error messages.
// This function is able to replace the original error messages.
// If this function is not set, the "Incorrect usage" is displayed and the execution is interrupted.
OnUsageError func(context *Context, err error, isSubcommand bool) error
// Compilation date
Compiled time.Time
// List of all authors who contributed
Expand Down Expand Up @@ -70,9 +77,10 @@ func compileTime() time.Time {
// Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
func NewApp() *App {
return &App{
Name: os.Args[0],
HelpName: os.Args[0],
Name: path.Base(os.Args[0]),
HelpName: path.Base(os.Args[0]),
Usage: "A new cli application",
UsageText: "",
Version: "0.0.0",
BashComplete: DefaultAppComplete,
Action: helpCommand.Action,
Expand Down Expand Up @@ -118,23 +126,26 @@ func (a *App) Run(arguments []string) (err error) {
set.SetOutput(ioutil.Discard)
err = set.Parse(arguments[1:])
nerr := normalizeFlags(a.Flags, set)
context := NewContext(a, set, nil)
if nerr != nil {
fmt.Fprintln(a.Writer, nerr)
context := NewContext(a, set, nil)
ShowAppHelp(context)
return nerr
}
context := NewContext(a, set, nil)

if checkCompletions(context) {
return nil
}

if err != nil {
fmt.Fprintln(a.Writer, "Incorrect Usage.")
fmt.Fprintln(a.Writer)
ShowAppHelp(context)
return err
if a.OnUsageError != nil {
err := a.OnUsageError(context, err, false)
return err
} else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowAppHelp(context)
return err
}
}

if !a.HideHelp && checkHelp(context) {
Expand All @@ -149,8 +160,7 @@ func (a *App) Run(arguments []string) (err error) {

if a.After != nil {
defer func() {
afterErr := a.After(context)
if afterErr != nil {
if afterErr := a.After(context); afterErr != nil {
if err != nil {
err = NewMultiError(err, afterErr)
} else {
Expand All @@ -161,8 +171,10 @@ func (a *App) Run(arguments []string) (err error) {
}

if a.Before != nil {
err := a.Before(context)
err = a.Before(context)
if err != nil {
fmt.Fprintf(a.Writer, "%v\n\n", err)
ShowAppHelp(context)
return err
}
}
Expand Down Expand Up @@ -238,10 +250,14 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
}

if err != nil {
fmt.Fprintln(a.Writer, "Incorrect Usage.")
fmt.Fprintln(a.Writer)
ShowSubcommandHelp(context)
return err
if a.OnUsageError != nil {
err = a.OnUsageError(context, err, true)
return err
} else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowSubcommandHelp(context)
return err
}
}

if len(a.Commands) > 0 {
Expand Down
Loading

0 comments on commit 69065e3

Please sign in to comment.