-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add tree & blob server commands
Replace show & list
- Loading branch information
1 parent
1f673f3
commit 4277403
Showing
5 changed files
with
195 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/soft-serve/git" | ||
"github.com/dustin/go-humanize" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// treeCommand returns a command that list file or directory at path. | ||
func treeCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "tree REPOSITORY [REFERENCE] [PATH]", | ||
Short: "Print repository tree at path.", | ||
Args: cobra.RangeArgs(1, 3), | ||
PersistentPreRunE: checkIfReadable, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, _ := fromContext(cmd) | ||
rn := args[0] | ||
path := "" | ||
ref := "" | ||
switch len(args) { | ||
case 2: | ||
path = args[1] | ||
case 3: | ||
ref = args[1] | ||
path = args[2] | ||
} | ||
rr, err := cfg.Backend.Repository(rn) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
r, err := rr.Open() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if ref == "" { | ||
head, err := r.HEAD() | ||
if err != nil { | ||
if bs, err := r.Branches(); err != nil && len(bs) == 0 { | ||
return fmt.Errorf("repository is empty") | ||
} | ||
return err | ||
} | ||
|
||
ref = head.Hash.String() | ||
} | ||
|
||
tree, err := r.LsTree(ref) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ents := git.Entries{} | ||
if path != "" && path != "/" { | ||
te, err := tree.TreeEntry(path) | ||
if err == git.ErrRevisionNotExist { | ||
return ErrFileNotFound | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if te.Type() == "tree" { | ||
tree, err = tree.SubTree(path) | ||
if err != nil { | ||
return err | ||
} | ||
ents, err = tree.Entries() | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
ents = append(ents, te) | ||
} | ||
} else { | ||
ents, err = tree.Entries() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
ents.Sort() | ||
for _, ent := range ents { | ||
size := ent.Size() | ||
ssize := "" | ||
if size == 0 { | ||
ssize = "-" | ||
} else { | ||
ssize = humanize.Bytes(uint64(size)) | ||
} | ||
cmd.Printf("%s\t%s\t %s\n", ent.Mode(), ssize, ent.Name()) | ||
} | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |