Skip to content

Commit

Permalink
new patch
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Dominic Della Valle <[email protected]>
  • Loading branch information
djdv committed Sep 17, 2018
1 parent 0ab9e85 commit b6c0dc4
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 44 deletions.
26 changes: 8 additions & 18 deletions cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (
"sort"
"strings"

"github.com/ipfs/go-ipfs-cmds"

osh "github.com/Kubuxu/go-os-helper"
"github.com/ipfs/go-ipfs-cmdkit"
"github.com/ipfs/go-ipfs-cmdkit/files"
"github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
)

Expand Down Expand Up @@ -79,9 +78,9 @@ func isRecursive(req *cmds.Request) bool {
return rec && ok
}

func derefLinks(req *cmds.Request) bool {
linkOpt, ok := req.Options[cmds.DerefLong].(bool)
return linkOpt && ok
func linkResolveDepth(req *cmds.Request) int {
linkOpt, _ := req.Options[cmds.DerefLong].(int) //TODO: safety concern
return linkOpt
}

type parseState struct {
Expand Down Expand Up @@ -270,7 +269,7 @@ func parseArgs(req *cmds.Request, root *cmds.Command, stdin *os.File) error {
fpath = stdin.Name()
file = files.NewReaderFile("", fpath, r, nil)
} else {
nf, err := appendFile(fpath, argDef, isRecursive(req), isHidden(req), derefLinks(req))
nf, err := appendFile(fpath, argDef, isRecursive(req), isHidden(req), linkResolveDepth(req))
if err != nil {
return err
}
Expand Down Expand Up @@ -460,7 +459,7 @@ func getArgDef(i int, argDefs []cmdkit.Argument) *cmdkit.Argument {
const notRecursiveFmtStr = "'%s' is a directory, use the '-%s' flag to specify directories"
const dirNotSupportedFmtStr = "Invalid path '%s', argument '%s' does not support directories"

func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden, derefLinks bool) (files.File, error) {
func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden bool, resolveDepth int) (files.File, error) {
if fpath == "." {
cwd, err := os.Getwd()
if err != nil {
Expand All @@ -475,16 +474,7 @@ func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden, derefL

fpath = filepath.ToSlash(filepath.Clean(fpath))

var (
stat os.FileInfo
err error
)
if derefLinks {
stat, err = os.Stat(fpath)
fmt.Printf("DBG appendFile stat: %#v\n", stat)
} else {
stat, err = os.Lstat(fpath)
}
stat, err := os.Lstat(fpath)
if err != nil {
return nil, err
}
Expand All @@ -498,7 +488,7 @@ func appendFile(fpath string, argDef *cmdkit.Argument, recursive, hidden, derefL
}
}

return files.NewSerialFile(path.Base(fpath), fpath, hidden, stat)
return files.NewSerialFile(path.Base(fpath), fpath, hidden, stat, resolveDepth)
}

// Inform the user if a file is waiting on input
Expand Down
1 change: 0 additions & 1 deletion cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func Run(ctx context.Context, root *cmds.Command,
if timeoutStr, ok := req.Options[cmds.TimeoutOpt]; ok {
timeout, err := time.ParseDuration(timeoutStr.(string))
if err != nil {
printErr(err)
return err
}
req.Context, cancel = context.WithTimeout(req.Context, timeout)
Expand Down
14 changes: 2 additions & 12 deletions http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package http

import (
"context"
"crypto/rand"
"encoding/base32"
"errors"
"net/http"
"runtime/debug"
Expand All @@ -12,6 +10,7 @@ import (

cmds "github.com/ipfs/go-ipfs-cmds"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-loggables"
cors "github.com/rs/cors"
)

Expand Down Expand Up @@ -133,7 +132,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
defer cancel()

req.Context = logging.ContextWithLoggable(req.Context, uuidLoggable())
req.Context = logging.ContextWithLoggable(req.Context, loggables.Uuid("requestId"))
if cn, ok := w.(http.CloseNotifier); ok {
clientGone := cn.CloseNotify()
go func() {
Expand Down Expand Up @@ -161,15 +160,6 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.root.Call(req, re, h.env)
}

func uuidLoggable() logging.Loggable {
ids := make([]byte, 16)
rand.Read(ids)

return logging.Metadata{
"requestId": base32.HexEncoding.EncodeToString(ids),
}
}

func sanitizedErrStr(err error) string {
s := err.Error()
s = strings.Split(s, "\n")[0]
Expand Down
2 changes: 0 additions & 2 deletions http/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ func parseResponse(httpRes *http.Response, req *cmds.Request) (cmds.Response, er
}
e.Message = string(mes)
e.Code = cmdkit.ErrNormal
case res.dec == nil:
return nil, fmt.Errorf("unknown error content type: %s", contentType)
default:
// handle marshalled errors
err := res.dec.Decode(&e)
Expand Down
10 changes: 0 additions & 10 deletions http/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package http

import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -198,15 +197,6 @@ func TestParseResponse(t *testing.T) {
},
},
},
{
status: 500,
header: http.Header{
contentTypeHeader: []string{"evil/bad"},
channelHeader: []string{"1"},
},
body: mkbuf("test error"),
err: fmt.Errorf("unknown error content type: %s", "evil/bad"),
},
}

for _, tc := range tcs {
Expand Down
2 changes: 1 addition & 1 deletion opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ var OptionEncodingType = cmdkit.StringOption(EncLong, EncShort, "The encoding ty
var OptionRecursivePath = cmdkit.BoolOption(RecLong, RecShort, "Add directory paths recursively").WithDefault(false)
var OptionStreamChannels = cmdkit.BoolOption(ChanOpt, "Stream channel output")
var OptionTimeout = cmdkit.StringOption(TimeoutOpt, "Set a global timeout on the command")
var OptionDerefArgs = cmdkit.BoolOption(DerefLong, "Resolve argument symlinks instead of adding them as-is").WithDefault(false)
var OptionDerefArgs = cmdkit.IntOption(DerefLong, "Resolve symlinks instead of adding them as-is").WithDefault(1)

0 comments on commit b6c0dc4

Please sign in to comment.