Skip to content

Timestamp flags don't work with environment variables #1755

@victorkt

Description

@victorkt

My urfave/cli version is

v2.25.6

Checklist

  • Are you running the latest v2 release? The list of releases is here.
  • Did you check the manual for your release? The v2 manual is here
  • Did you perform a search about this problem? Here's the GitHub guide about searching.

Dependency Management

  • My project is using go modules.
  • My project is using vendoring.

Describe the bug

Timestamp flags don't seem to work with environment variables. When passing the value as an env var, the value is unchanged. This doesn't happen when passing values as arguments.

To reproduce

I've tested it with the following code:

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/urfave/cli/v2"
)

var someTimestampFlag cli.Timestamp

func main() {
	app := cli.NewApp()
	app.Flags = []cli.Flag{
		&cli.TimestampFlag{
			Name:        "some-timestamp-flag",
			EnvVars:     []string{"SOME_TIMESTAMP_FLAG"},
			Layout:      time.RFC3339,
			Destination: &someTimestampFlag,
			Required:    true,
		},
	}
	app.Action = func(c *cli.Context) error {
		fmt.Println("env var:", os.Getenv("SOME_TIMESTAMP_FLAG"))
		fmt.Println("flag:", someTimestampFlag.Value())
		return nil
	}
	if err := app.Run(os.Args); err != nil {
		log.Fatal(err)
	}
}

Observed behavior

$ SOME_TIMESTAMP_FLAG=2021-03-02T06:20:00Z go run main.go
env var: 2021-03-02T06:20:00Z
flag: <nil>

$ go run main.go --some-timestamp-flag=2021-03-02T06:20:00Z
env var: 
flag: 2021-03-02 06:20:00 +0000 UTC

Expected behavior

I'd expect the flag contained the provided date in both runs.

Run go version and paste its output here

$ go version
go version go1.20.5 darwin/arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/v2relates to / is being considered for v2kind/bugdescribes or fixes a bugstatus/triagemaintainers still need to look into this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions