func DurationInterceptor()

in internal/commands/interceptor/duration.go [55:91]


func DurationInterceptor(cliCtx *cli.Context) error {
	start := cliCtx.String("start")
	end := cliCtx.String("end")
	userStep := cliCtx.Generic("step")
	if timezone := cliCtx.String("timezone"); timezone != "" {
		if offset, err := strconv.Atoi(timezone); err == nil {
			// `offset` is in form of "+1300", while `time.FixedZone` takes offset in seconds
			time.Local = time.FixedZone("", offset/100*60*60)
		}
	}

	var s api.Step
	if userStep != nil {
		s = userStep.(*model.StepEnumValue).Selected
	}

	startTime, endTime, step, dt := ParseDuration(start, end, s)

	if err := cliCtx.Set("start", startTime.Format(utils.StepFormats[step])); err != nil {
		return err
	} else if err := cliCtx.Set("end", endTime.Format(utils.StepFormats[step])); err != nil {
		return err
	} else if err := cliCtx.Set("step", step.String()); err != nil {
		return err
	} else if err := cliCtx.Set("duration-type", dt.String()); err != nil {
		return err
	}

	ctx := cliCtx.Context
	ctx = context.WithValue(ctx, contextkey.DurationStart{}, startTime.Format(utils.StepFormats[step]))
	ctx = context.WithValue(ctx, contextkey.DurationEnd{}, endTime.Format(utils.StepFormats[step]))
	ctx = context.WithValue(ctx, contextkey.DurationStep{}, step)
	ctx = context.WithValue(ctx, contextkey.DurationType{}, utils.DurationType(dt.String()))
	cliCtx.Context = ctx

	return nil
}