Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with importing #561

Closed
b5y opened this issue Nov 10, 2016 · 9 comments
Closed

Problem with importing #561

b5y opened this issue Nov 10, 2016 · 9 comments

Comments

@b5y
Copy link

b5y commented Nov 10, 2016

Hello,

My question is quite similar to #473 issue, #512 and #511 PRs.
I have read documentation and want to implement simple application.
My working area is (building from terminal):

  1. Mac OS (Version 10.12)
  2. Go version: 1.7.3

I want to read specific fields from config.yml and parse it.
Here is my code (hope I am on the right way):

package main

import (
	"fmt"
	"log"
	"os"
	"gopkg.in/urfave/cli.v1"
	"gopkg.in/urfave/cli.v1/altsrc"
)

func main() {
	app := cli.NewApp()
	app.Name = "sizer"
	app.Usage = "utility to show file's size in megabytes"
	app.Version = "0.0.1"

	app.Commands = []cli.Command{
		{
			Name: "size",
			Aliases: []string{"s"},
			Usage: "showing file's size",
			Action: func(c *cli.Context) error {
				fmt.Println("File's size: ", GetFileSize(c.Args().First()))
				return nil
			},
			Description: "file size",
			Flags: []cli.Flag{
				altsrc.NewStringFlag(cli.StringFlag{Name: "file"}),
				cli.StringFlag{Name: "load"}},
		},
	}
	app.Before = altsrc.InitInputSourceWithContext(app.Flags, altsrc.NewYamlSourceFromFlagFunc("load"))
	app.Run(os.Args)
}

// Returns file size in megabytes
func GetFileSize(fileName string) int {
	file, err := os.Open(fileName)
	// Check if file is not empty. If it is, then exit from program
	if err != nil {
		fmt.Println("Problem with getting time execution")
		log.Println(err)
		os.Exit(1)
	}
	// Useful to close file after getting information about it
	defer file.Close()
	fileInfo, err := file.Stat()
	if err != nil {
		fmt.Println("Error getting file's size in TimeToExecute function")
		log.Println(err)
		os.Exit(1)
	}
	// Get file size in MB
	var fileSize int
	size := fileInfo.Size() / (1024 * 1024)
	fileSize = int(size)
	return fileSize
}

Which outputs after go build *.go these errors:

# command-line-arguments
./main.go:28: cannot use "gopkg.in/urfave/cli.v1".StringFlag literal (type "gopkg.in/urfave/cli.v1".StringFlag) as type "github.com/urfave/cli".StringFlag in argument to altsrc.NewStringFlag
./main.go:32: cannot use app.Flags (type []"gopkg.in/urfave/cli.v1".Flag) as type []"github.com/urfave/cli".Flag in argument to altsrc.InitInputSourceWithContext
./main.go:32: cannot use altsrc.InitInputSourceWithContext(app.Flags, altsrc.NewYamlSourceFromFlagFunc("load")) (type "github.com/urfave/cli".BeforeFunc) as type "gopkg.in/urfave/cli.v1".BeforeFunc in assignment

I have removed github.com/urfave/cli package with these commands and nothing has changed:

go clean -i -n github.com/urfave/cli...
go clean -i github.com/urfave/cli...
rmdir $GOPATH/pkg/darwin_amd64/github.com/urfave/cli
rm -rf $GOPATH/src/darwin_amd64/github.com/urfave/cli
screen shot 2016-11-10 at 17 58 19

@jszwedko
Copy link
Contributor

Hi @b5y,

Thank you for bringing this to my attention. Even though #512 and #511 were merged, I neglected that they won't take effect until a new release is cut (given how gopkg.in only pulls released tags). I'm in the process of cutting one now so this issue should be resolved shortly.

@b5y
Copy link
Author

b5y commented Nov 13, 2016

Hi @jszwedko,

You are welcome. Thank you for response and work!

@jszwedko
Copy link
Contributor

Just published a release, so this should be fixed now, but please let me know if you still see issues.

@b5y
Copy link
Author

b5y commented Nov 14, 2016

Thank you very much! I will give response this week as soon as possible.

@b5y
Copy link
Author

b5y commented Nov 21, 2016

Sorry for late answer. Now I got this error:

../../../Go/src/gopkg.in/urfave/cli.v1/altsrc/flag.go:10:2: cannot find package "github.com/urfave/cli" in any of:
	/usr/local/opt/go/libexec/src/github.com/urfave/cli (from $GOROOT)
	/Users/<user_name>/Go/src/github.com/urfave/cli (from $GOPATH)

Why we use gopkg.in? Does only github not enough for deploying and installing project? This leads for some problems in installing and using framework.

@jszwedko
Copy link
Contributor

gopkg.in was introduced to give people an easy way to pin dependencies to package versions (in response to #473). It is becoming slightly less relevant now that vendoring is becoming more supported by Go as a pattern, but since there isn't yet a generally adopted Go dependency management framework, I think it is still useful to end users.

What go get command did you run that gave you that error? I was able to run the following successfully:

go get -u gopkg.in/urfave/cli/altsrc.v1
go get -u gopkg.in/urfave/cli.v1/altsrc 

@dkushner
Copy link

dkushner commented Jun 7, 2017

@jszwedko, I'm also having this issue having originally installed the cli dependency from Github. When I try to run your commands, I get:

package gopkg.in/urfave/cli/altsrc.v1: unrecognized import path "gopkg.in/urfave/cli/altsrc.v1" (parse https://gopkg.in/urfave/cli/altsrc.v1?go-get=1: no go-import meta tags ())

@jszwedko
Copy link
Contributor

jszwedko commented Jun 25, 2017

@dkushner apologies for the confusion, go get -u gopkg.in/urfave/cli.v1/altsrc is the correct command. gopkg.in/urfave/cli/altsrc.v1 looks to be unsupported by gopkg.in.

@coilysiren
Copy link
Member

Hiya! There's a known issue with the current implementation of the CLI, where altsrc is generally clunky and poorly documented. My current idea is that I'm going to move all of the altsrc functionality into the main package in some way, and that's how all of the "altsrc is weird and different???" issues people are having will get resolved. I'm tracking that work here => #833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants