Ejemplo n.º 1
0
func (mfc *mockFileCreator) CreateFile(path string) (*os.File, error) {
	out, err := os.Create(path)
	if err != nil {
		return out, err
	}

	return out, nil
}

var _ = Describe("Dictionary", func() {
	mf := &mockFetcher{}
	mfc := &mockFileCreator{}

	It("Returns error on empty URL", func() {
		err := fetch.Dictionary("", "../data/test", mf, mfc)
		Expect(err).To(Not(BeNil()))
	})
	It("Successfully retrieves valid URL", func() {
		err := fetch.Dictionary("derp", "../data/test", mf, mfc)
		Expect(err).To(BeNil())
	})
	It("Returns error when a folder does not exist", func() {
		err := fetch.Dictionary("derp", "fakefolder/test", mf, mfc)
		Expect(err).To(Not(BeNil()))
	})
})

var _ = Describe("FileCreator", func() {
	It("Returns error when a folder does not exist", func() {
		fc := fetch.FileCreator{}
Ejemplo n.º 2
0
package cmd

import (
	"github.com/nii236/jmdict-toolkit/fetch"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

// fetchCmd represents the fetch command
var fetchCmd = &cobra.Command{
	Use:   "fetch",
	Short: "Fetches the JMDICT file from the internet",
	Long:  "Fetches the JMDICT file from the internet",
	Run: func(cmd *cobra.Command, args []string) {
		f := &fetch.Fetcher{}
		fc := &fetch.FileCreator{}
		fetch.Dictionary(viper.GetString("url"), viper.GetString("outfile"), f, fc)
	},
}

func init() {
	RootCmd.AddCommand(fetchCmd)
	fetchCmd.Flags().StringP("url", "u", "ftp://ftp.monash.edu.au/pub/nihongo/JMdict_e.gz", "HTTP path to the JMDICT file")
	fetchCmd.Flags().StringP("outfile", "o", "data/JMdict_e.gz", "Location to save the dictionary")
	viper.BindPFlags(fetchCmd.Flags())
}