Skip to content

Commit

Permalink
Merge pull request #61 from k1LoW/completion
Browse files Browse the repository at this point in the history
Add `hrv completion`
  • Loading branch information
k1LoW authored Jan 11, 2020
2 parents e5e18e9 + ec72b89 commit ca15a88
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 18 deletions.
7 changes: 6 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ brews:
email: k1lowxb@gmail.com
homepage: https://github.com/k1LoW/harvest
description: Portable log aggregation tool for middle-scale system operation/observation.
install: bin.install 'hrv'
install: |
system 'hrv', 'completion', 'bash', '--out', 'hrv.bash'
system 'hrv', 'completion', 'zsh', '--out', 'hrv.zsh'
bin.install 'hrv'
bash_completion.install "hrv.bash" => "hrv"
zsh_completion.install "hrv.zsh" => "_hrv"
nfpms:
-
id: harvest-nfpms
Expand Down
88 changes: 88 additions & 0 deletions cmd/hrv/cmd/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright © 2020 Ken'ichiro Oyama <k1lowxb@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var outFile string

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Short: "output shell completion code",
Long: `output shell completion code.
To configure your shell to load completions for each session add to your *shrc
# bash
echo '. <(hrv completion bash)' > ~/.bashrc
# zsh
hrv completion zsh > $fpath[1]/_hrv
`,
ValidArgs: []string{"bash", "zsh"},
Args: func(cmd *cobra.Command, args []string) error {
n := 1
if len(args) != n {
return fmt.Errorf("accepts %d arg(s), received %d", n, len(args))
}
if err := cobra.OnlyValidArgs(cmd, args); err != nil {
return err
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
var err error
sh := args[0]
out := os.Stdout
if outFile != "" {
out, err = os.Create(outFile)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
defer out.Close()
}

switch sh {
case "bash":
if err := rootCmd.GenBashCompletion(out); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
case "zsh":
if err := rootCmd.GenZshCompletion(out); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
},
}

func init() {
rootCmd.AddCommand(completionCmd)
completionCmd.Flags().StringVarP(&outFile, "out", "o", "", "output file path")
}
59 changes: 42 additions & 17 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"fmt"
"testing"
"time"
)
Expand All @@ -10,41 +11,65 @@ var parseTimeTests = []struct {
tz string
content string
want string
}{
{
"02/Jan/2006:15:04:05 -0700",
"",
"04/Feb/2019:00:13:49 +0900",
"2019-02-04T00:13:49.000000000 +09:00",
},
{
"02/Jan/2006:15:04:05 -0700",
"+0500",
"04/Feb/2019:00:13:49 +0900",
"2019-02-04T00:13:49.000000000 +09:00",
},
}

func TestParseTime(t *testing.T) {
for _, tt := range parseTimeTests {
got, err := parseTime(tt.tf, tt.tz, tt.content)
if err != nil {
t.Errorf("%v", err)
}
want, err := time.Parse("2006-01-02T15:04:05.999999999 -07:00", tt.want)
if err != nil {
t.Errorf("%v", err)
}
if got.UnixNano() != want.UnixNano() {
t.Errorf("\ngot %s\nwant %s", got, want)
}
}
}

var parseTimeDetectionTests = []struct {
tf string
tz string
content string
want string
}{
{
"Jan 02 15:04:05",
"+0900",
"Mar 05 23:59:59",
"2019-03-05T23:59:59.000000000 +09:00",
fmt.Sprintf("%d-%s", time.Now().Year(), "03-05T23:59:59.000000000 +09:00"),
},
{
"Jan 02 15:04:05",
"+0000",
"Mar 05 23:59:59",
"2019-03-05T23:59:59.000000000 +00:00",
fmt.Sprintf("%d-%s", time.Now().Year(), "03-05T23:59:59.000000000 +00:00"),
},
{
"Jan 02 15:04:05",
"",
"Mar 05 23:59:59",
"2019-03-05T23:59:59.000000000 +00:00",
},
{
"02/Jan/2006:15:04:05 -0700",
"",
"04/Feb/2019:00:13:49 +0900",
"2019-02-04T00:13:49.000000000 +09:00",
},
{
"02/Jan/2006:15:04:05 -0700",
"+0500",
"04/Feb/2019:00:13:49 +0900",
"2019-02-04T00:13:49.000000000 +09:00",
fmt.Sprintf("%d-%s", time.Now().Year(), "03-05T23:59:59.000000000 +00:00"),
},
}

func TestParseTime(t *testing.T) {
for _, tt := range parseTimeTests {
func TestParseTimeDetection(t *testing.T) {
for _, tt := range parseTimeDetectionTests {
got, err := parseTime(tt.tf, tt.tz, tt.content)
if err != nil {
t.Errorf("%v", err)
Expand Down

0 comments on commit ca15a88

Please sign in to comment.