46 lines
881 B
Go
46 lines
881 B
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/spf13/cobra"
|
|
"huffman/internal/huffman"
|
|
"os"
|
|
)
|
|
|
|
var decodeCmd = &cobra.Command{
|
|
Use: "decode <archive name> <file name>",
|
|
Short: "Decodes the file",
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
if len(args) < 2 {
|
|
return errors.New("filename is required")
|
|
}
|
|
|
|
if _, err := os.Stat(args[0]); os.IsNotExist(err) {
|
|
return errors.New("archive file is not exists")
|
|
} else if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if _, err := os.Stat(args[1]); err == nil {
|
|
return errors.New("output file exists")
|
|
} else if !os.IsNotExist(err) {
|
|
panic(err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
fromFile, err := os.Open(args[0])
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
toFile, err := os.Create(args[1])
|
|
|
|
huffman.Decode(fromFile, toFile)
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(decodeCmd)
|
|
}
|