All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.plugins.buildmetrics.system.labels.OsLabelsFactory.kt Maven / Gradle / Ivy

The newest version!
package ru.tinkoff.plugins.buildmetrics.system.labels

import ru.tinkoff.plugins.buildmetrics.api.factories.Factory
import ru.tinkoff.plugins.buildmetrics.api.labels.Label
import ru.tinkoff.plugins.buildmetrics.system.utils.SystemManufacturer
import ru.tinkoff.plugins.buildmetrics.system.utils.SystemManufacturerImpl
import ru.tinkoff.plugins.buildmetrics.utils.system.SystemProperties
import ru.tinkoff.plugins.buildmetrics.utils.system.SystemPropertiesImpl

/**
 * Labels:
 * - os_arch;
 * - os_name;
 * - os_version;
 * - os_manufacturer;
 *
 * @throws IllegalStateException if system property not found.
 */
class OsLabelsFactory(
    private val systemProperties: SystemProperties = SystemPropertiesImpl(),
    private val systemManufacturer: SystemManufacturer = SystemManufacturerImpl(),
) : Factory.Labels {

    override fun create(): List> = listOf(
        osArchLabel(),
        osNameLabel(),
        osVersionLabel(),
        osManufacturerLabel(),
    )

    /**
     * Visible for testing.
     *
     * @return label with os arch value.
     */
    internal fun osArchLabel(): Label = Label(
        name = "os_arch",
        value = systemProperty("os.arch"),
    )

    /**
     * Visible for testing.
     *
     * @return label with os name value.
     */
    internal fun osNameLabel(): Label = Label(
        name = "os_name",
        value = systemProperty("os.name"),
    )

    /**
     * Visible for testing.
     *
     * @return label with os version value.
     */
    internal fun osVersionLabel(): Label = Label(
        name = "os_version",
        value = systemProperty("os.version"),
    )

    /**
     * Visible for testing.
     *
     * @return label with os manufacturer value.
     */
    internal fun osManufacturerLabel(): Label = Label(
        name = "os_manufacturer",
        value = systemManufacturer.manufacturer(),
    )

    /**
     * @return system property.
     * @throws IllegalStateException if property not found.
     */
    private fun systemProperty(property: String): String =
        systemProperties.property(property) ?: error("Failed to get property '$property'")

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy