Skip to content

Commit

Permalink
cmd/verify: Implement verify command
Browse files Browse the repository at this point in the history
  • Loading branch information
pg9182 committed Apr 14, 2024
1 parent 1a80c5d commit 4a833cd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

_ "github.com/pg9182/tf2vpk/cmd/cat"
_ "github.com/pg9182/tf2vpk/cmd/lzham"
_ "github.com/pg9182/tf2vpk/cmd/verify"
_ "github.com/pg9182/tf2vpk/cmd/version"
)

Expand Down
72 changes: 72 additions & 0 deletions cmd/verify/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package verify

import (
"fmt"
"io"
"os"

"github.com/pg9182/tf2vpk"
"github.com/pg9182/tf2vpk/cmd/root"
"github.com/spf13/cobra"
)

var Flags struct {
VPK tf2vpk.ValvePak
Files []string
Verbose bool
}

var Command = &cobra.Command{
Use: "verify vpk_path",
Short: "Verifies the contents of the specified VPK.",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Flags.Files = args[1:]
main()
},
}

func init() {
root.ArgVPK(&Flags.VPK, Command, -1, false, false, false)
Command.Flags().BoolVarP(&Flags.Verbose, "verbose", "v", false, "display files as they are verified")
root.Command.AddCommand(Command)
}

func main() {
r, err := tf2vpk.NewReader(Flags.VPK)
if err != nil {
fmt.Fprintf(os.Stderr, "error: open vpk: %v\n", err)
os.Exit(1)
}

var failure int
for _, f := range r.Root.File {
if Flags.Verbose {
fmt.Printf("%s: ", f.Path)
os.Stderr.Sync()
}
if err := func() error {
r, err := r.OpenFileParallel(f, root.Flags.Threads)
if err != nil {
return err
}
if _, err := io.Copy(io.Discard, r); err != nil {
return err
}
return nil
}(); err != nil {
if Flags.Verbose {
fmt.Printf("ERROR\n")
}
fmt.Fprintf(os.Stderr, "%s: ERROR - %v\n", f.Path, err)
failure++
} else {
if Flags.Verbose {
fmt.Printf("OK\n")
}
}
}
if failure != 0 {
os.Exit(1)
}
}

0 comments on commit 4a833cd

Please sign in to comment.