Skip to content

Commit

Permalink
Add digest subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
tiziano88 committed Oct 4, 2022
1 parent 7874ef4 commit 0acf327
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nixEnvSelector.nixFile": "${workspaceRoot}/shell.nix"
}
72 changes: 72 additions & 0 deletions cmd/ent/cmd/digest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Copyright 2022 The Ent Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"io/ioutil"
"log"
"os"

"github.com/fatih/color"
"github.com/google/ent/utils"
"github.com/spf13/cobra"
)

var digestCmd = &cobra.Command{
Use: "digest [filename]",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filename := ""
if len(args) > 0 {
filename = args[0]
}
if filename == "" {
err := digestStdin()
if err != nil {
log.Fatal(err)
}
} else {
err := digestFile(filename)
if err != nil {
log.Fatal(err)
}
}

},
}

func digestStdin() error {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("could not read stdin: %v", err)
}
return digestData(data)
}

func digestFile(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("could not read file %q: %v", filename, err)
}
return digestData(data)
}

func digestData(data []byte) error {
localDigest := utils.ComputeDigest(data)
fmt.Printf("%s\n", color.YellowString(string(localDigest)))
return nil
}
1 change: 1 addition & 0 deletions cmd/ent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func Execute() {
}

func init() {
rootCmd.AddCommand(digestCmd)
rootCmd.AddCommand(getCmd)
rootCmd.AddCommand(putCmd)
rootCmd.AddCommand(statusCmd)
Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
pkgs.mkShell {
nativeBuildInputs = [
pkgs.nodePackages.prettier
pkgs.buildPackages.go
];
}

0 comments on commit 0acf327

Please sign in to comment.