|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "image" |
| 7 | + "math/rand" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/action" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/action/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | + "github.com/qeesung/image2ascii/convert" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + _ action.Action = (*printBufo)(nil) |
| 18 | +) |
| 19 | + |
| 20 | +func NewPrintBufo() action.Action { |
| 21 | + return &printBufo{} |
| 22 | +} |
| 23 | + |
| 24 | +type printBufo struct{} |
| 25 | + |
| 26 | +func (a *printBufo) Metadata(ctx context.Context, req action.MetadataRequest, resp *action.MetadataResponse) { |
| 27 | + resp.TypeName = req.ProviderTypeName + "_print" |
| 28 | +} |
| 29 | + |
| 30 | +func (a *printBufo) Schema(ctx context.Context, req action.SchemaRequest, resp *action.SchemaResponse) { |
| 31 | + resp.Schema = schema.UnlinkedSchema{ |
| 32 | + Description: "Prints an ASCII bufo", |
| 33 | + Attributes: map[string]schema.Attribute{ |
| 34 | + "name": schema.StringAttribute{ |
| 35 | + Description: "Name of the bufo to print, see: https://bufo.zone/. If no name is provided, a random bufo will be selected!", |
| 36 | + Optional: true, |
| 37 | + Validators: []validator.String{ |
| 38 | + ValidBufoName(), |
| 39 | + }, |
| 40 | + }, |
| 41 | + "ratio": schema.Float64Attribute{ |
| 42 | + Description: "The ratio to scale the width/height of the bufo from the original, defaults to 0.5", |
| 43 | + Optional: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (a *printBufo) Invoke(ctx context.Context, req action.InvokeRequest, resp *action.InvokeResponse) { |
| 50 | + var config printBufoModel |
| 51 | + |
| 52 | + resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) |
| 53 | + if resp.Diagnostics.HasError() { |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + var bufoFileName string |
| 58 | + if config.Name.IsNull() { |
| 59 | + // Choose a random bufo |
| 60 | + bufoEntries, err := bufos.ReadDir("bufos") |
| 61 | + if err != nil { |
| 62 | + resp.Diagnostics.AddError( |
| 63 | + "Failed to select a random bufo", |
| 64 | + fmt.Sprintf("There was an issue finding a random bufo: %s", err), |
| 65 | + ) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + randomBufoIdx := rand.Intn(len(bufoEntries)) |
| 70 | + bufoFileName = bufoEntries[randomBufoIdx].Name() |
| 71 | + } else { |
| 72 | + bufoFileName = fmt.Sprintf("%s.png", config.Name.ValueString()) |
| 73 | + } |
| 74 | + |
| 75 | + bufoLocation := fmt.Sprintf("bufos/%s", bufoFileName) |
| 76 | + |
| 77 | + bufoFile, err := bufos.Open(bufoLocation) |
| 78 | + if err != nil { |
| 79 | + resp.Diagnostics.AddError( |
| 80 | + "Failed to retrieve bufo", |
| 81 | + fmt.Sprintf("There was an issue finding bufo at %q: %s", bufoFileName, err), |
| 82 | + ) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + converter := convert.NewImageConverter() |
| 87 | + img, _, err := image.Decode(bufoFile) |
| 88 | + if err != nil { |
| 89 | + resp.Diagnostics.AddError( |
| 90 | + "Failed to decode bufo", |
| 91 | + fmt.Sprintf("There was an issue decoding bufo at %q: %s", bufoFileName, err), |
| 92 | + ) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + ratio := 0.5 |
| 97 | + if !config.Ratio.IsNull() { |
| 98 | + ratio = config.Ratio.ValueFloat64() |
| 99 | + } |
| 100 | + |
| 101 | + bufoAscii := converter.Image2ASCIIString(img, &convert.Options{ |
| 102 | + Ratio: ratio, |
| 103 | + FixedWidth: -1, |
| 104 | + FixedHeight: -1, |
| 105 | + }) |
| 106 | + |
| 107 | + resp.SendProgress(action.InvokeProgressEvent{ |
| 108 | + Message: fmt.Sprintf("\n\nBufo: %q\n\n%s", bufoFileName, bufoAscii), |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +type printBufoModel struct { |
| 113 | + Name types.String `tfsdk:"name"` |
| 114 | + Ratio types.Float64 `tfsdk:"ratio"` |
| 115 | +} |
0 commit comments