joshmedeski / sesh

Smart session manager for the terminal
MIT License
410 stars 16 forks source link

Taplo Schema Support #113

Open joshmedeski opened 1 month ago

joshmedeski commented 1 month ago

What would you like sesh to do?

As a user, I want taplo to offer my schema definition, so I can more easily create and modify sesh configuration.

Create schema file

To create a schema definition for a TOML file using Taplo, follow these steps:

  1. Create a new file with a .toml extension for your schema, e.g., schema.toml.

  2. Open the schema.toml file and define your schema using the Taplo schema language. Here's an example schema:

[TOML schema]
title = "My Schema"
description = "A schema for my TOML configuration file"

[section1]
key1 = { type = "string", required = true }
key2 = { type = "integer", minimum = 0, maximum = 100 }

[section2]
key3 = { type = "boolean" }
key4 = { type = "array", items = { type = "string" } }

In this example:

  1. Save the schema.toml file.

  2. In your main TOML configuration file, add a reference to the schema file using the __schema key:

__schema = "schema.toml"

[section1]
key1 = "value1"
key2 = 50

[section2]
key3 = true
key4 = ["item1", "item2"]
  1. Taplo will now validate your TOML configuration file against the defined schema.

For more information on Taplo's schema language and available validation options, refer to the [Taplo documentation](https://taplo.tamasfe.dev/configuration/schema.html).

Integrate with brew

Yes, you can configure the schema file to be installed via Homebrew and automatically detected based on the file's location. Here's how you can achieve this:

  1. Create a Homebrew formula for your schema file. Let's say you have a schema file named myapp.toml.schema and you want to install it using Homebrew. Create a new file named myapp-schema.rb with the following content:
class MyappSchema < Formula
  desc "Schema for MyApp TOML configuration"
  homepage "https://example.com/myapp"
  url "https://example.com/myapp/myapp.toml.schema"
  sha256 "SCHEMA_FILE_SHA256_HASH"

  def install
    share.install "myapp.toml.schema"
  end
end

Replace SCHEMA_FILE_SHA256_HASH with the actual SHA256 hash of your schema file.

  1. Install the schema using Homebrew:
brew install ./myapp-schema.rb

This will install the schema file in the Homebrew shared directory, typically /usr/local/share/myapp.toml.schema.

  1. In your application, you can automatically detect the schema file based on its location. Taplo supports schema auto-discovery by looking for schema files in specific locations relative to the TOML file being validated.

By default, Taplo looks for schema files in the following locations (in order):

Where <filename> is the name of your TOML configuration file.

  1. To use the installed schema file, you can symlink it to one of the supported locations. For example, if your TOML configuration file is named config.toml, you can create a symlink in the same directory:
ln -s /usr/local/share/myapp.toml.schema config.toml.schema.toml

Now, when Taplo validates your config.toml file, it will automatically discover and use the schema file installed via Homebrew.

By following these steps, you can install the schema file using Homebrew and have Taplo automatically detect and use it based on the file's location.