Skip to content

Commit

Permalink
added basic logging
Browse files Browse the repository at this point in the history
- enabled when environment variable `CARAPACE_LOG` is set
- logs to {tmpdir}/carapace/{binaryname}.log
  • Loading branch information
rsteube committed Nov 19, 2020
1 parent ef343d5 commit 76c2de9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ commands:
jobs:
go-current:
docker:
- image: circleci/golang:1.12
- image: circleci/golang:1.15
environment:
GO111MODULE: "on"
working_directory: *workspace
Expand All @@ -46,7 +46,7 @@ jobs:
command: diff -u <(echo -n) <(gofmt -d -s .)
go-previous:
docker:
- image: circleci/golang:1.11
- image: circleci/golang:1.14
environment:
GO111MODULE: "on"
working_directory: *workspace
Expand Down
44 changes: 34 additions & 10 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package carapace
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"

Expand Down Expand Up @@ -112,11 +115,13 @@ func addCompletionCommand(cmd *cobra.Command) {
Use: "_carapace",
Hidden: true,
Run: func(cmd *cobra.Command, args []string) {
logger.Println(os.Args) // TODO replace last with '' if empty

if len(args) == 0 {
if s, err := Gen(cmd).Snippet(determineShell(), true); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(io.MultiWriter(os.Stderr, logger.Writer()), err.Error())
} else {
fmt.Println(s)
fmt.Fprintln(io.MultiWriter(os.Stdout, logger.Writer()), s)
}
} else {
if len(args) == 1 {
Expand All @@ -127,9 +132,9 @@ func addCompletionCommand(cmd *cobra.Command) {
}
default:
if s, err := Gen(cmd).Snippet(args[0], false); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintln(io.MultiWriter(os.Stderr, logger.Writer()), err.Error())
} else {
fmt.Println(s)
fmt.Fprintln(io.MultiWriter(os.Stdout, logger.Writer()), s)
}
}
} else {
Expand All @@ -143,16 +148,16 @@ func addCompletionCommand(cmd *cobra.Command) {
if _uid, action, ok := findAction(targetCmd, targetArgs); ok {
CallbackValue = uid.Value(targetCmd, targetArgs, _uid)
if action.callback == nil {
fmt.Println(action.Value(shell))
fmt.Fprintln(io.MultiWriter(os.Stdout, logger.Writer()), action.Value(shell))
} else {
fmt.Println(action.callback(targetArgs).nestedAction(targetArgs, 2).Value(shell))
fmt.Fprintln(io.MultiWriter(os.Stdout, logger.Writer()), action.callback(targetArgs).nestedAction(targetArgs, 2).Value(shell))
}
}
case "state":
fmt.Println(uid.Command(targetCmd))
fmt.Fprintln(io.MultiWriter(os.Stdout, logger.Writer()), uid.Command(targetCmd))
default:
CallbackValue = uid.Value(targetCmd, targetArgs, id)
fmt.Println(completions.invokeCallback(id, targetArgs).nestedAction(targetArgs, 2).Value(shell))
fmt.Fprintln(io.MultiWriter(os.Stdout, logger.Writer()), completions.invokeCallback(id, targetArgs).nestedAction(targetArgs, 2).Value(shell))
}
}
}
Expand Down Expand Up @@ -226,8 +231,6 @@ func determineShell() string {
return "xonsh"
case "zsh":
return "zsh"
default:
return ""
}
}
}
Expand All @@ -236,3 +239,24 @@ func determineShell() string {
func IsCallback() bool {
return len(os.Args) > 3 && os.Args[1] == "_carapace" && os.Args[3] != "state"
}

var logger = log.New(ioutil.Discard, "", log.Flags())

func init() {
if _, enabled := os.LookupEnv("CARAPACE_LOG"); enabled {
if err := initLogger(); err != nil {
log.Fatal(err.Error())
}
}
}

func initLogger() (err error) {
tmpdir := fmt.Sprintf("%v/carapace", os.TempDir())
if err = os.MkdirAll(tmpdir, os.ModePerm); err == nil {
var logfileWriter io.Writer
if logfileWriter, err = os.OpenFile(fmt.Sprintf("%v/%v.log", tmpdir, uid.Executable()), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666); err == nil {
logger = log.New(logfileWriter, determineShell()+" ", log.Flags()|log.Lmsgprefix)
}
}
return
}

0 comments on commit 76c2de9

Please sign in to comment.