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

Sort variables based on the order in the code files #35

Open
sfenman opened this issue Apr 14, 2023 · 2 comments
Open

Sort variables based on the order in the code files #35

sfenman opened this issue Apr 14, 2023 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@sfenman
Copy link

sfenman commented Apr 14, 2023

Hello,

Thanks for this tool, it really helps. What I would like to see as a feature is to be able to sort variables based on the order they are found in the code files eg. in the main.tf. There are sometimes where I want to "group" variables based on the resource type.

For example let's assume that I have an AWS S3 module. I might have a logical block inside main.tf that defines all IAM resources before the actual AWS S3 resource. Sorting variables in this order enhances navigation in the variables.tf file especially when you have multiple types of Terraform resources.

I understand this is a huge and time consuming implementation but if you think there is a fit in this project, I could help with that.

@AlexNabokikh
Copy link
Owner

@sfenman I appreciate your idea and recognize the effort it would take to bring it to fruition.
However, given my current workload, I must prioritize my primary responsibilities.
If you have the opportunity and inclination to work on this initiative, I encourage you to do so, and I am open to any possible contributions you may have.

@AlexNabokikh AlexNabokikh added enhancement New feature or request help wanted Extra attention is needed labels Apr 17, 2023
@jamesgeddes
Copy link
Contributor

I have come across a similar challenge, so to address this, I implemented a consistent naming convention for variables that inherently groups them by provider, resource type, and logical grouping, mirroring their appearance in the Terraform code.

Proposed Naming Convention

The naming convention I used, and found highly effective, is:

[provider]_[group]_[resource]_[property]

  • provider: The cloud provider or system (e.g., aws, gcp, azurerm).
  • group: A logical grouping of resources, such as network, compute, iam, or storage.
  • resource: The specific resource type (e.g., bucket, role, instance, policy).
  • property: The specific attribute of the resource (e.g., name, role_arn, acl, tags).

This structure creates a natural grouping and sorting of variables, making navigation intuitive without needing changes to any tools.

Example:

Here’s how this convention could look for AWS IAM and S3 resources:

variable "aws_iam_role_name" {
  description = "The name of the IAM role"
  type        = string
}

variable "aws_iam_policy_arn" {
  description = "The ARN of the IAM policy"
  type        = string
}

variable "aws_storage_s3_bucket_name" {
  description = "The name of the S3 bucket"
  type        = string
}

variable "aws_storage_s3_bucket_acl" {
  description = "The ACL to apply to the S3 bucket"
  type        = string
  default     = "private"
}

Here’s why this works well:

  1. Logical Grouping: The group field (iam, storage, etc.) ensures related variables appear together, following the structure of main.tf.
  2. Explicit and Descriptive: The convention clearly reflects the resource, its grouping, and its specific property, improving code quality and readability.
  3. Order Mirrors main.tf: This aligns variables in variables.tf with the logical blocks in main.tf, making navigation seamless.
  4. Scalability: This approach scales well for projects with multiple resource types or multiple providers.

Why Naming Conventions Help

This naming convention eliminates the need for additional tools or features while achieving the same result:

  1. Tool-Agnostic: It works with any editor or tool, including tfsort.
  2. Improves Code Quality: The explicit nature of the naming convention enhances clarity, making the code more maintainable and intuitive for others.
  3. Collaboration-Friendly: Teams can adopt this structure for consistent project-wide organisation, reducing confusion and improving onboarding.
  4. No change: This would not require any changes to tfsort.

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants