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

fix: Adding a missing data type for Time #267

Merged
merged 11 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions cmd/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log/slog"
"os"

"github.com/TylerBrock/colorjson"
"github.com/spf13/cobra"

"github.com/blackstork-io/fabric/engine"
Expand Down Expand Up @@ -51,14 +50,7 @@ var dataCmd = &cobra.Command{
return
}
val := res.Any()
var ser []byte
if cliArgs.colorize {
fmt := colorjson.NewFormatter()
fmt.Indent = 4
ser, err = fmt.Marshal(val)
} else {
ser, err = json.MarshalIndent(val, "", " ")
}
ser, err := json.MarshalIndent(val, "", " ")
if diags.AppendErr(err, "Failed to serialize data output to json") {
return
}
Expand Down
20 changes: 10 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ var (
rootCtx context.Context
cliArgs = struct {
sourceDir string
colorize bool
noColor bool
debug bool
}{}
rawArgs = struct {
sourceDir string
logOutput string
logLevel string
verbose bool
colorize bool
noColor bool
debug bool
}{}
debugDir = ".fabric/debug"
Expand All @@ -64,7 +64,7 @@ func init() {
&rawArgs.logLevel, "log-level", "info",
fmt.Sprintf("logging level (%s)", validLogLevels.String()),
)
rootCmd.PersistentFlags().BoolVar(&rawArgs.colorize, "color", true, "enables colorizing the logs and diagnostics (if supported by the terminal and log format)")
rootCmd.PersistentFlags().BoolVar(&rawArgs.noColor, "no-color", false, "disable colorization of the output (logs and diagnostics), if supported by the terminal and the log format")
rootCmd.PersistentFlags().BoolVarP(&rawArgs.verbose, "verbose", "v", false, "a shortcut to --log-level debug")
rootCmd.PersistentFlags().BoolVar(&rawArgs.debug, "debug", false, "enables debug mode")

Expand Down Expand Up @@ -101,7 +101,7 @@ var rootCmd = &cobra.Command{
return
}
cliArgs.sourceDir = rawArgs.sourceDir
cliArgs.colorize = rawArgs.colorize && term.IsTerminal(int(os.Stderr.Fd()))
cliArgs.noColor = rawArgs.noColor && term.IsTerminal(int(os.Stderr.Fd()))
var level slog.Level
if rawArgs.verbose || rawArgs.debug {
level = slog.LevelDebug
Expand All @@ -119,17 +119,17 @@ var rootCmd = &cobra.Command{
var handler slog.Handler
switch strings.ToLower(strings.TrimSpace(rawArgs.logOutput)) {
case "plain":
if cliArgs.colorize && level <= slog.LevelDebug {
if !cliArgs.noColor && level <= slog.LevelDebug {
handler = devslog.NewHandler(os.Stderr, &devslog.Options{
HandlerOptions: opts,
})
} else {
var output io.Writer
if cliArgs.colorize {
if cliArgs.noColor {
output = os.Stderr
} else {
// only affects windows, noop on *nix
output = colorable.NewColorable(os.Stderr)
} else {
output = os.Stderr
}

handler = tint.NewHandler(
Expand All @@ -138,7 +138,7 @@ var rootCmd = &cobra.Command{
AddSource: opts.AddSource,
Level: opts.Level,
ReplaceAttr: opts.ReplaceAttr,
NoColor: !cliArgs.colorize,
NoColor: cliArgs.noColor,
TimeFormat: time.DateTime,
},
)
Expand Down Expand Up @@ -218,6 +218,6 @@ func exitCommand(eng *engine.Engine, cmd *cobra.Command, diags diagnostics.Diag)
} else {
err = nil
}
eng.PrintDiagnostics(os.Stderr, diags, cliArgs.colorize)
eng.PrintDiagnostics(os.Stderr, diags, !cliArgs.noColor)
return err
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2
github.com/Masterminds/semver/v3 v3.2.1
github.com/Masterminds/sprig/v3 v3.2.3
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
github.com/blackstork-io/goldmark-markdown v0.1.3
github.com/crowdstrike/gofalcon v0.8.0
github.com/elastic/go-elasticsearch/v8 v8.14.0
Expand Down Expand Up @@ -117,7 +116,6 @@ require (
github.com/gorilla/css v1.0.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ github.com/Microsoft/hcsshim v0.11.5 h1:haEcLNpj9Ka1gd3B3tAEs9CpE0c+1IhoL59w/exY
github.com/Microsoft/hcsshim v0.11.5/go.mod h1:MV8xMfmECjl5HdO7U/3/hFVnkmSBjAjmA09d4bExKcU=
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
Expand Down Expand Up @@ -172,8 +170,6 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4=
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA=
Expand Down
43 changes: 3 additions & 40 deletions internal/builtin/data_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,11 @@ func fetchJSONData(ctx context.Context, params *plugin.RetrieveDataParams) (plug
}

func readAndDecodeJSONFile(path string) (plugindata.Data, error) {
file, err := os.Open(path)
jsonData, err := os.ReadFile(path)
if err != nil {
return nil, err
}
defer file.Close()

var content jsonData
err = json.NewDecoder(file).Decode(&content)
if err != nil {
file.Close()
return nil, err
}
return content.data, nil
return plugindata.UnmarshalJSON(jsonData)
}

func readJSONFiles(ctx context.Context, pattern string) (plugindata.List, error) {
Expand Down Expand Up @@ -158,36 +150,7 @@ type jsonData struct {
}

func (d jsonData) toData(v any) (res plugindata.Data, err error) {
switch v := v.(type) {
case nil:
return nil, nil
case float64:
return plugindata.Number(v), nil
case string:
return plugindata.String(v), nil
case bool:
return plugindata.Bool(v), nil
case map[string]any:
m := make(plugindata.Map)
for k, v := range v {
m[k], err = d.toData(v)
if err != nil {
return nil, err
}
}
return m, nil
case []any:
l := make(plugindata.List, len(v))
for i, v := range v {
l[i], err = d.toData(v)
if err != nil {
return nil, err
}
}
return l, nil
default:
return nil, fmt.Errorf("unsupported type %T", v)
}
return plugindata.ParseAny(v)
}

func (d *jsonData) UnmarshalJSON(b []byte) error {
Expand Down
59 changes: 2 additions & 57 deletions internal/builtin/data_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@ package builtin

import (
"context"
"fmt"
"log/slog"
"os"
"path/filepath"

"github.com/hashicorp/hcl/v2"
"github.com/zclconf/go-cty/cty"
"gopkg.in/yaml.v3"

"github.com/blackstork-io/fabric/pkg/diagnostics"
"github.com/blackstork-io/fabric/plugin"
"github.com/blackstork-io/fabric/plugin/dataspec"
"github.com/blackstork-io/fabric/plugin/plugindata"
)

type yamlData struct {
data plugindata.Data
}

func makeYAMLDataSource() *plugin.DataSource {
return &plugin.DataSource{
DataFunc: fetchYAMLData,
Expand Down Expand Up @@ -121,16 +115,11 @@ func fetchYAMLData(ctx context.Context, params *plugin.RetrieveDataParams) (plug
}

func readAndDecodeYAMLFile(path string) (plugindata.Data, error) {
yamlFile, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var content yamlData
err = yaml.Unmarshal(yamlFile, &content)
yamlData, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return content.data, nil
return plugindata.UnmarshalYAML(yamlData)
}

func readYAMLFiles(ctx context.Context, pattern string) (plugindata.List, error) {
Expand All @@ -157,47 +146,3 @@ func readYAMLFiles(ctx context.Context, pattern string) (plugindata.List, error)
}
return result, nil
}

func (d yamlData) toData(v any) (res plugindata.Data, err error) {
switch v := v.(type) {
case nil:
return nil, nil
case int:
return plugindata.Number(v), nil
case float64:
return plugindata.Number(v), nil
case string:
return plugindata.String(v), nil
case bool:
return plugindata.Bool(v), nil
case map[string]any:
m := make(plugindata.Map)
for k, v := range v {
m[k], err = d.toData(v)
if err != nil {
return nil, err
}
}
return m, nil
case []any:
l := make(plugindata.List, len(v))
for i, v := range v {
l[i], err = d.toData(v)
if err != nil {
return nil, err
}
}
return l, nil
default:
return nil, fmt.Errorf("can't convert type %T into `plugindata.Data`", v)
}
}

func (d *yamlData) UnmarshalYAML(node *yaml.Node) (err error) {
var result any
if err := node.Decode(&result); err != nil {
return err
}
d.data, err = d.toData(result)
return err
}
12 changes: 6 additions & 6 deletions internal/github/publish_github_gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func publishGithubGist(loader ClientLoaderFn) plugin.PublishFunc {
if document == nil {
return diagnostics.Diag{{
Severity: hcl.DiagError,
Summary: "Failed to parse document",
Summary: "Failed to parse the document",
Detail: "document is required",
}}
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func publishGithubGist(loader ClientLoaderFn) plugin.PublishFunc {
if !descriptionAttr.IsNull() && descriptionAttr.AsString() != "" {
payload.Description = gh.String(descriptionAttr.AsString())
}
slog.InfoContext(ctx, "Publish to github gist", "filename", fileName)
slog.InfoContext(ctx, "Publishing to GitHub gist", "filename", fileName)
gistId := params.Args.GetAttrVal("gist_id")
if gistId.IsNull() || gistId.AsString() == "" {
slog.DebugContext(ctx, "No gist id set, creating a new gist", "is_public", payload.Public, "files", len(payload.Files))
Expand All @@ -149,14 +149,14 @@ func publishGithubGist(loader ClientLoaderFn) plugin.PublishFunc {
Detail: err.Error(),
}}
}
slog.InfoContext(ctx, "Created gist", "url", *gist.HTMLURL)
slog.InfoContext(ctx, "Created the gist", "url", *gist.HTMLURL)
} else {
slog.DebugContext(ctx, "Fetching the gist", "gist_id", gistId.AsString())
gist, _, err := client.Gists().Get(ctx, gistId.AsString())
if err != nil {
return diagnostics.Diag{{
Severity: hcl.DiagError,
Summary: "Failed to retreive gist",
Summary: "Failed to retreive the gist",
Detail: err.Error(),
}}
}
Expand All @@ -173,11 +173,11 @@ func publishGithubGist(loader ClientLoaderFn) plugin.PublishFunc {
if err != nil {
return diagnostics.Diag{{
Severity: hcl.DiagError,
Summary: "Failed to update gist",
Summary: "Failed to update the gist",
Detail: err.Error(),
}}
}
slog.InfoContext(ctx, "Updated gist", "url", *gist.HTMLURL)
slog.InfoContext(ctx, "The gist updated successfully", "url", *gist.HTMLURL)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/blackstork-io/fabric/pkg/utils/slogutil"
)

type Diag hcl.Diagnostics // Diagnostics does implement error interface, but not, itself, an error.
type Diag hcl.Diagnostics // Diagnostics does implement error interface, but is not, itself, an error.

func (d Diag) Error() string {
slog.Debug("Treated diagnostic.Diag as error", slogutil.SourceOverride(1))
Expand Down
Loading
Loading