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

Support outputting a list of databases as JSON #112

Merged

Conversation

kate-goldenring
Copy link
Collaborator

Takes a similar approach as spin templates list and provides an option of listing databases as JSON

Example output:

[
  {
    "database": "brilliant-kangaroo",
    "links": [
      {
        "label": "default",
        "app": "todo"
      }
    ]
  },
  {
    "database": "mydb",
    "links": [
      {
        "label": "hello",
        "app": "todo"
      },
      {
        "label": "foo",
        "app": "bar"
      }
    ]
  }
]

@kate-goldenring
Copy link
Collaborator Author

@itowlson I noticed that in spin templates list, we hide the optionality of json output format. Is there a reason for that -- and one that is related to whether it should be hidden here too?

@itowlson
Copy link
Contributor

itowlson commented Oct 2, 2023

@kate-goldenring Just to reduce clutter in the help text. In that case, the JSON is primarily expected to be consumed by other programs, not be interactive users, so I felt it was not worth making interactive users look at the flag.

Copy link
Contributor

@itowlson itowlson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good (style nit aside) but I think it needs to either respect the --app filter or (less desirably) be incompatible with it.

src/commands/sqlite.rs Outdated Show resolved Hide resolved
src/commands/sqlite.rs Show resolved Hide resolved
src/commands/sqlite.rs Outdated Show resolved Hide resolved
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
Copy link
Contributor

@itowlson itowlson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Looks good!

Copy link
Contributor

@rylev rylev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine though there does seem to be unnecessary code duplication between the json and table formatting code paths.

src/commands/sqlite.rs Outdated Show resolved Hide resolved
Comment on lines 239 to 248
if let Some(app) = &self.app {
databases.retain(|d| {
d.links
.iter()
.any(|l| l.app_name.as_deref().unwrap_or("UNKNOWN") == app)
});
}
if let Some(db) = &self.database {
databases.retain(|d| d.name == *db);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems we're repeating a lot of the same logic here between json and table printing. Why not delay the formatting decision until we've munged all the data?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One is iterating over Links while the other is over Databases since the tabular form does one row per link while the json form does one array entry per database. If we want to do links as teh output format for json, we could share code but i think it makes more sense in json to represent a database and then nest all its links

src/commands/sqlite.rs Outdated Show resolved Hide resolved
src/commands/sqlite.rs Outdated Show resolved Hide resolved
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
@kate-goldenring kate-goldenring merged commit c8619db into fermyon:main Oct 4, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants